WPF中的样式按钮

时间:2011-07-13 10:56:13

标签: wpf xaml styles

我对button样式符合以下代码。

<Style TargetType="Button" x:Key="TransparentButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border HorizontalAlignment="Center" x:Name="borderTemplate"
                        Background="Transparent">
                    <ContentPresenter/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="borderTemplate"
                                Property="Border.BorderBrush" Value="Gray" />
                        <Setter TargetName="borderTemplate"
                                Property="Border.BorderThickness" Value="1" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="borderTemplate"
                                Property="Border.BorderBrush" Value="Lime" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我需要设置background

button

1.一旦发生“ MouseUp ”事件,就灰色

2.只要按钮被更改或者说焦点丢失,就将其设置为默认值(比如白色)。

有没有办法使用TriggerEventTrigger来解决这个问题。


请参阅上面代码

的一个示例

以上风格的按钮。

 <Button Background="Black" Style="{StaticResource TransparentButton}"
         Content="0123456789" Height="15" HorizontalAlignment="Left"
         Name="button2" VerticalAlignment="Top" Margin="70,17,0,0" />

在应用上方样式后,按钮将以下图片

enter image description here

1 个答案:

答案 0 :(得分:2)

这可以通过使用RadioButton而不是Button来完成 看看这个

<Style x:Key="RadioButtonStyle1" TargetType="{x:Type RadioButton}">
    <Setter Property="Background" Value="#F4F4F4"/>
    <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RadioButton}">
                 <Border  HorizontalAlignment="Center" x:Name="borderTemplate" Background="Transparent" VerticalAlignment="Center">
                            <ContentPresenter x:Name="contentPresenter" VerticalAlignment="Center"/>
                        </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" TargetName="borderTemplate" Value="#FFE4E4E4"/>
                        <Setter Property="HorizontalAlignment" TargetName="contentPresenter" Value="Center"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Background" TargetName="borderTemplate" Value="#FFA1A1A1"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Grid x:Name="LayoutRoot">
    <StackPanel/>
    <RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="116,86.04,0,0" Style="{StaticResource RadioButtonStyle1}"/>
    <RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="116,106,0,0" Style="{StaticResource RadioButtonStyle1}"/>
</Grid>
相关问题