1.解释说明
- 通过设置style资源词典可以批量设置控件,不仅节省大量时间,还能方便统一修改
- 重复利用Border这个控件,可以自由设计新的控件风格
- 这里要注意,虽然style也是写在xaml文件中,但是其文件类型为资源词典类型,这里程序示例也进行不同的展开。
2.style资源词典程序示例
- x:Key="ModeStyle" 即我对这个格式命名为ModeStyle
- TargetType="RadioButton" 即适用的控件类型为RadioButton
- Property="Background" 即要修改的属性为背景
- Value="#80FFFFFF" 即为要设置的值为半透明的白色
- 里面有个特殊应用,就是在RadioButton新建了一个新RadioButton,同时在新RadioButton里面建立了一个boeder控件,这个控件的背景颜色绑定为最初的RadioButton上
- 当按钮按下时(IsChecked = true)改变颜色为#FF000068,当鼠标移动到按键上方时(IsMouseOver = true)改变颜色为LightBlue
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--文字与视图按钮-->
<Style x:Key="ModeStyle" TargetType="RadioButton">
<Setter Property="Background" Value="#80FFFFFF" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Height" Value="35" />
<Setter Property="Width" Value="100" />
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Cursor" Value="Hand "/>
<Setter Property="GroupName" Value="txtview"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<!-- 定义按钮的视觉结构 -->
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="8">
<!-- 倒角边框 -->
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<!-- 当RadioButton被选中时的样式 -->
<Setter Property="Background" Value="#FF000068"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- 更改其他需要改变的属性 -->
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightBlue"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
3.前端界面引用
- <ResourceDictionary Source="Controls.xaml"/> 即为资源词典的引用,资源字典的文件名为“Controls”
- Style="{DynamicResource ModeStyle}" 即将资源词典风格设置到对应控件上
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- 引入Controls.xaml资源字典 -->
<ResourceDictionary Source="Controls.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<!--行排布-->
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition Height="65"/>
<RowDefinition Height="45"/>
<RowDefinition Height="65"/>
</Grid.RowDefinitions>
<!--排布按钮-->
<Grid Height="Auto" Grid.Row="2">
<StackPanel Orientation="Horizontal" >
<RadioButton x:Name="1" lan:Xaml.Key="1" Margin="60,0,0,0" Style="{DynamicResource ModeStyle}" Cursor="Hand" />
<RadioButton x:Name="2" lan:Xaml.Key="2" Margin="20,0,0,0" Style="{DynamicResource ModeStyle}" />
<RadioButton x:Name="3" lan:Xaml.Key="3" Margin="20,0,0,0" Style="{DynamicResource ModeStyle}" />
<RadioButton x:Name="4" lan:Xaml.Key="4" Margin="20,0,0,0" Style="{DynamicResource ModeStyle}" />
</StackPanel>
</Grid>
</Grid>
标签:控件,style,ModeStyle,RadioButton,小白,WPF,词典,资源
From: https://blog.csdn.net/Vae2437426397/article/details/140359776