WPF中的styling按钮
我有一个样式用于WPF中的styling按钮,wpf,xaml,styles,Wpf,Xaml,Styles,我有一个样式用于按钮,如下代码所示 <Style TargetType="Button" x:Key="TransparentButton"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border HorizontalAlignment="
按钮
,如下代码所示
<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>
应用上述样式后,按钮将看起来与下图一样。
<Button Background="Black" Style="{StaticResource TransparentButton}"
Content="0123456789" Height="15" HorizontalAlignment="Left"
Name="button2" VerticalAlignment="Top" Margin="70,17,0,0" />
这可以通过使用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>
感谢您给出答案……我已经尝试过这个代码……但它的结果是……只要我按下按钮上的鼠标……它就会将边框设置为石灰,将背景设置为灰色……**当鼠标点击事件发生时,应该是灰色的???背景应该是灰色的……谢谢……。对于演示,您可以看到SilverLight的数据寻呼机控件…这里是..你可以看到那边..导航到那篇文章的演示部分..谢谢..我用RadioButton而不是按钮样式更新了我的答案请看一看..谢谢..回答..但是这里发生了什么变化..所有按钮都变成了灰色
,除了..选中的一个。。。谢谢…非常感谢你的回答…但是。。。我找不到StaticResourceCheckBoxStroke
我已将其设置为…。尽管当鼠标按在按钮上时,边框未更改。。。。