How do I style Buttons

前端 未结 2 1676
生来不讨喜
生来不讨喜 2020-12-31 08:47

\"Button

\"Button

My U

相关标签:
2条回答
  • 2020-12-31 09:15

    The MSDN documentation on Button Styles will probably be of help to you. It gives an example of a WPF Button Template that you should be able to edit to your requirements. You can put the Xaml in Windows.Resources if the style is only going to be used on that Form, or you can edit the Application.xaml file and put the Style information in the Application.Resources Section if it it is to be used for the entire application.


    Modified above style link to give you an example:

    <Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml">
        <Application.Resources>
            <!--Control colors.-->
            <Color x:Key="ControlNormalColor">#FFC0C0CE</Color>
            <Color x:Key="ControlMouseOverColor">#FFAFA3B9</Color>
            <Color x:Key="DisabledControlColor">#FFF2F2F2</Color>
            <Color x:Key="DisabledForegroundColor">#FFBFBFBF</Color>
            <Color x:Key="ControlPressedColor">#FF211AA9</Color>
    
            <!-- FocusVisual -->
    
            <Style x:Key="ButtonFocusVisual">
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border>
                                <Rectangle Margin="2" StrokeThickness="1" Stroke="#60000000" StrokeDashArray="1 2" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
            <!-- Button -->
            <Style TargetType="Button">
                <Setter Property="SnapsToDevicePixels" Value="true" />
                <Setter Property="OverridesDefaultStyle" Value="true" />
                <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}" />
                <Setter Property="MinHeight" Value="29px" />
                <Setter Property="MinWidth"  Value="103px" />
                <Setter Property="Foreground" Value="#FFFFFFFF" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border TextBlock.Foreground="{TemplateBinding Foreground}" x:Name="Border">
                                <Border.Background>
                                    <SolidColorBrush  Color="{DynamicResource ControlNormalColor}" />
                                </Border.Background>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualStateGroup.Transitions>
                                            <VisualTransition GeneratedDuration="0:0:0.5" />
                                            <VisualTransition GeneratedDuration="0" To="Pressed" />
                                        </VisualStateGroup.Transitions>
                                        <VisualState x:Name="Normal" />
                                        <VisualState x:Name="MouseOver">
                                            <Storyboard>
                                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                                                    Storyboard.TargetName="Border">
                                                    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlMouseOverColor}" />
                                                </ColorAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Pressed">
                                            <Storyboard>
                                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                                                    Storyboard.TargetName="Border">
                                                    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlPressedColor}" />
                                                </ColorAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                                                    Storyboard.TargetName="Border">
                                                    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledControlColor}" />
                                                </ColorAnimationUsingKeyFrames>
                                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                                    Storyboard.TargetName="Border">
                                                    <EasingColorKeyFrame KeyTime="0" Value="{StaticResource DisabledForegroundColor}" />
                                                </ColorAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <ContentPresenter Margin="2"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                RecognizesAccessKey="True" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
        </Application.Resources>
    </Application>
    
    0 讨论(0)
  • 2020-12-31 09:20

    Use styles to customize your UI controls.

    See the following links for examples:

    http://wpftutorial.net/Styles.html

    http://geekswithblogs.net/cskardon/archive/2008/06/20/roundedbutton-button-style-wpf.aspx

    http://gregandora.wordpress.com/2011/02/06/wpf-minimal-button-styling/

    0 讨论(0)
提交回复
热议问题