How to create/make rounded corner buttons in WPF?

前端 未结 12 2115
礼貌的吻别
礼貌的吻别 2020-12-04 11:39

I need to create a rounded corner glossy button in WPF. Can anyone please explain me what steps are needed?

相关标签:
12条回答
  • 2020-12-04 11:57

    This is an adapted version of @Kishore Kumar 's answer that is simpler and more closely matches the default button style and colours. It also fixes the issue that his "IsPressed" trigger is in the wrong order and will never be executed since the "MouseOver" will take precedent:

    <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="grid">
                            <Border x:Name="border" CornerRadius="2" BorderBrush="#707070" BorderThickness="1" Background="LightGray">
                                <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          TextElement.FontWeight="Normal">
                                </ContentPresenter>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="border" Value="#BEE6FD"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="#3C7FB1"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="BorderBrush" TargetName="border" Value="#2C628B"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" TargetName="grid" Value="0.25"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
    
    0 讨论(0)
  • 2020-12-04 12:03

    You can try this...........

     <Border BorderBrush="Black" Name="MyBorder"  
                Height="78" 
                Background="Red" 
                Width="74" 
                CornerRadius="3">
            <Button Width="{Binding MyBorder.Width}" 
                    Height="{Binding MyBorder.Height}" 
                    Content="Hi" Background="Red"/>
        </Border>
    
    0 讨论(0)
  • 2020-12-04 12:11

    Well the best way to get round corners fast and with standard animation is to create a copy of the control template with Blend. Once you get a copy set the corner radius on the Grid tag and you should be able to have your control with full animation functionality and applyable to any button control. look this is the code:

    <ControlTemplate x:Key="ButtonControlTemplate" TargetType="Button">        
        <Grid x:Name="RootGrid" Background="{TemplateBinding Background}"
              CornerRadius="8,8,8,8">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal">
                        <Storyboard>
                            <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="PointerOver">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPointerOver}" />
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPressed}" />
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPointerOver}" />
                            </ObjectAnimationUsingKeyFrames>
                            <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Pressed">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundPressed}" />
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushPressed}" />
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundPressed}" />
                            </ObjectAnimationUsingKeyFrames>
                            <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Disabled">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBackgroundDisabled}" />
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonBorderBrushDisabled}" />
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonForegroundDisabled}" />
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <!--<Border CornerRadius="8,8,8,8"
                            Background="#002060"
                            BorderBrush="Red"
                            BorderThickness="2">-->
                <ContentPresenter x:Name="ContentPresenter"
                                  BorderBrush="{TemplateBinding BorderBrush}"
                                  BorderThickness="{TemplateBinding BorderThickness}"
                                  Content="{TemplateBinding Content}"
                                  ContentTransitions="{TemplateBinding ContentTransitions}"
                                  ContentTemplate="{TemplateBinding ContentTemplate}"
                                  Padding="{TemplateBinding Padding}"
                                  HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                  VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                  AutomationProperties.AccessibilityView="Raw"/>
            <!--</Border>-->
        </Grid>        
    </ControlTemplate>
    

    I also edited the VisualState="PointerOver" specifically at Storyboard.TargetName="BorderBrush", because its ThemeResource get squared corners whenever PointerOver triggers.

    Then you should be able to apply it to your control style like this:

    <Style TargetType="ContentControl" x:Key="ButtonLoginStyle"
           BasedOn="{StaticResource CommonLoginStyleMobile}">
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Background" Value="#002060"/>
        <Setter Property="Template" Value="{StaticResource ButtonControlTemplate}"/>        
    </Style>
    

    So you can apply your styles to any Button.

    0 讨论(0)
  • 2020-12-04 12:13

    Although this question is long-since answered, I used an alternative approach that people might find simpler than any of these solutions (even Keith Stein's excellent answer). So I'm posting it in case it might help anyone.

    You can achieve rounded corners on a button without having to write any XAML (other than a Style, once) and without having to replace the template or set/change any other properties. Just use an EventSetter in your style for the button's "Loaded" event and change it in code-behind.

    (And if your style lives in a separate Resource Dictionary XAML file, then you can put the event code in a code-behind file for your resource dictionary.)

    I do it like this:

    Xaml Style:

    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
        <EventSetter Event="Loaded"                   Handler="ButtonLoaded"/>
    </Style>
    

    Code-Behind:

    public partial class ButtonStyles
    {
        private void ButtonLoaded(object sender, RoutedEventArgs e)
        {
            if (!(sender is Button b))
                return;
    
            // Find the first top-level border we can.
    
            Border border = default;
            for (var i = 0; null == border && i < VisualTreeHelper.GetChildrenCount(b); ++i)
                border = VisualTreeHelper.GetChild(b, i) as Border;
    
            // If we found it, set its corner radius how we want.  
    
            if (border != null)
                border.CornerRadius = new CornerRadius(3);
        }
    }
    

    If you had to add the code-behind file to an existing resource dictionary xaml file, you can even have the code-behind file automatically appear underneath that XAML file in the Visual Studio Solution if you want. In a .NET Core project, just give it appropriate corresponding name (e.g if the resource Dictionary is "MyDictionary.xaml", name the code-behind file "MyDictionary.xaml.cs"). In a .NET Framework project, you need to edit the .csproj file in XML mode

    0 讨论(0)
  • 2020-12-04 12:15

    I know this post is super old, but I have an answer that's surprisingly missing from the above and is also much simpler than most.

    <Button>
        <Button.Resources>
            <Style TargetType="Border">
                <Setter Property="CornerRadius" Value="5"/>
            </Style>
        </Button.Resources>
    </Button>
    

    Since the default ControlTemplate for the Button control uses a Border element, adding a style for Border to the Button's resources applies that style to that Border. This lets you add rounded corners without having to make your own ControlTemplate and without any code. It also works on all varieties of Button (e.g. ToggleButton and RepeatButton).

    0 讨论(0)
  • 2020-12-04 12:15

    in your app.xaml add this part of style :

       <Application.Resources>
         <Style TargetType="FrameworkElement" x:Key="VisibleAnimation">
      <Setter Property="Visibility" Value="Collapsed"/>
      <Setter Property="Opacity" Value="10"/>
      <Setter Property="Height" Value="700"></Setter>
      <Style.Triggers>
        <Trigger Property="Visibility" Value="Visible">
          <Trigger.EnterActions>
        <BeginStoryboard>
          <Storyboard>
            <DoubleAnimation Storyboard.TargetProperty="Opacity"
                             From="0.0" To="1.0" Duration="0:0:0.5"/>
           </Storyboard>
         </BeginStoryboard>
       </Trigger.EnterActions>
       </Trigger>
     </Style.Triggers>
    </Style>
    
        <Style TargetType="Button" x:Key="BTNCORNER">
            <Setter Property="Background" Value="White" />
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="7,7,7,7" Background="White"    BorderBrush="#ccc" BorderThickness="1,1,1,1" >
                            <ContentPresenter x:Name="contentPresenter"   ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding  Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding  VerticalContentAlignment}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        </Application.Resources>
    

    Button

     <Button x:Name="loginButton"  
             Style="{StaticResource BTNCORNER}"   
             Margin="50,20,20,20"  
             Click="loginButton_Click" 
             FontSize="20" Width="93" Height="42"  />
    
    0 讨论(0)
提交回复
热议问题