How to create/make rounded corner buttons in WPF?

前端 未结 12 2114
礼貌的吻别
礼貌的吻别 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:49
    <Button x:Name="btnBack" Grid.Row="2" Width="300"
                            Click="btnBack_Click">
                    <Button.Template>
                        <ControlTemplate>
                            <Border CornerRadius="10" Background="#463190">
                                <TextBlock Text="Retry" Foreground="White" 
                                           HorizontalAlignment="Center"                                           
                                           Margin="0,5,0,0"
                                           Height="40"
                                           FontSize="20"></TextBlock>
                            </Border>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
    

    This is working fine for me.

    0 讨论(0)
  • 2020-12-04 11:50

    This is more of a minimal control template to get a button with rounded corners, however you will not have any hover or click visual effects. But you can add these to the control template as needed. I was working with a dark background, hence the white background.

    <Style x:Key="RoundedButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="15" Background="White" BorderThickness="1" Padding="2">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    I used the control template from the following blog post as my starting point: http://shihac-sharp.blogspot.com.au/2012/05/button-with-rounded-corners-in-wpf.html

    0 讨论(0)
  • 2020-12-04 11:51

    As alternative, you can code something like this:

        <Border 
                x:Name="borderBtnAdd"
                BorderThickness="1" 
                BorderBrush="DarkGray" 
                CornerRadius="360" 
                Height="30" 
                Margin="0,10,10,0" 
                VerticalAlignment="Top" HorizontalAlignment="Right" Width="30">
            <Image x:Name="btnAdd"
                   Source="Recursos/Images/ic_add_circle_outline_black_24dp_2x.png"
                   Width="{Binding borderBtnAdd.Width}" Height="{Binding borderBtnAdd.Height}"/>
        </Border>
    

    The "Button" will look something like this:

    You could set any other content instead of the image.

    0 讨论(0)
  • 2020-12-04 11:52

    You can use attached properties for setting button border radius (also the same will work for textboxes).

    Create class for attached property

    public class CornerRadiusSetter
    {
        public static CornerRadius GetCornerRadius(DependencyObject obj) => (CornerRadius)obj.GetValue(CornerRadiusProperty);
    
        public static void SetCornerRadius(DependencyObject obj, CornerRadius value) => obj.SetValue(CornerRadiusProperty, value);
    
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.RegisterAttached(nameof(Border.CornerRadius), typeof(CornerRadius),
                typeof(CornerRadiusSetter), new UIPropertyMetadata(new CornerRadius(), CornerRadiusChangedCallback));
    
        public static void CornerRadiusChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
        {
            Control control = sender as Control;
    
            if (control == null) return;
    
            control.Loaded -= Control_Loaded;
            control.Loaded += Control_Loaded;
        }
    
        private static void Control_Loaded(object sender, EventArgs e)
        {
            Control control = sender as Control;
    
            if (control == null || control.Template == null) return;
    
            control.ApplyTemplate();
    
            Border border = control.Template.FindName("border", control) as Border;
    
            if (border == null) return;
    
            border.CornerRadius = GetCornerRadius(control);
        }
    }
    

    Then you can use attached property syntax for multiple buttons without style duplicates:

    <Button local:CornerRadiusSetter.CornerRadius="10">Click me!</Button>
    <Button local:CornerRadiusSetter.CornerRadius="5, 0, 0, 5">Click me!</Button>
    <Button local:CornerRadiusSetter.CornerRadius="3, 20, 8, 15">Click me!</Button>
    
    0 讨论(0)
  • You have to create your own ControlTemplate for the Button. just have a look at the sample

    created a style called RoundCorner and inside that i changed rather created my own new Control Template with Border (CornerRadius=8) for round corner and some background and other trigger effect. If you have or know Expression Blend it can be done very easily.

    <Style x:Key="RoundCorner" TargetType="{x:Type Button}">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="grid">
                        <Border x:Name="border" CornerRadius="8" BorderBrush="Black" BorderThickness="2">
                            <Border.Background>
                                <RadialGradientBrush GradientOrigin="0.496,1.052">
                                    <RadialGradientBrush.RelativeTransform>
                                        <TransformGroup>
                                            <ScaleTransform CenterX="0.5" CenterY="0.5" 
                                                            ScaleX="1.5" ScaleY="1.5"/>
                                            <TranslateTransform X="0.02" Y="0.3"/>
                                        </TransformGroup>
                                    </RadialGradientBrush.RelativeTransform>
                                    <GradientStop Offset="1" Color="#00000000"/>
                                    <GradientStop Offset="0.3" Color="#FFFFFFFF"/>
                                </RadialGradientBrush>
                            </Border.Background>
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              TextElement.FontWeight="Bold">
                            </ContentPresenter>
                        </Border>
    
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <RadialGradientBrush GradientOrigin="0.496,1.052">
                                        <RadialGradientBrush.RelativeTransform>
                                            <TransformGroup>
                                                <ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.5" ScaleY="1.5"/>
                                                <TranslateTransform X="0.02" Y="0.3"/>
                                            </TransformGroup>
                                        </RadialGradientBrush.RelativeTransform>
                                        <GradientStop Color="#00000000" Offset="1"/>
                                        <GradientStop Color="#FF303030" Offset="0.3"/>
                                    </RadialGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF33962B"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" TargetName="grid" Value="0.25"/>
                        </Trigger>
    
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    Using

    <Button Style="{DynamicResource RoundCorner}" 
            Height="25" 
            VerticalAlignment="Top" 
            Content="Show" 
            Width="100" 
            Margin="5" />
    
    0 讨论(0)
  • 2020-12-04 11:57

    The simplest solution without changing the default style is:

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

    Then just define your button like this:

    <Button Style="{DynamicResource RoundButton}" />
    
    0 讨论(0)
提交回复
热议问题