标题
郝伟 2021/0/0
WPF UI元素类型
https://www.bbsmax.com/A/GBJr6EbBJ0/
https://www.cnblogs.com/xiongyang123/p/10682782.html
xmlns:local=“clr-namespace:WpfApplication2”
这篇文章很好,测试可用: https://blog.csdn.net/qq_34802416/article/details/78231575
定义了类型-值的成对数据资源,然后再使用,具体步骤:
<Window x:Class="StyleTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:StyleTest" xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" <Window.Resources> <FontFamily x:Key="ButtonFontFamily">Times New Roman</FontFamily> <sys:Double x:Key="ButtonFontSize">18</sys:Double> <FontWeight x:Key="ButtonFontWeight">Bold</FontWeight> </Window.Resources> <StackPanel VerticalAlignment="Center"> <Button Name="cmd" Content="Resource Button" Width="150" Height="30" Margin="3" FontFamily="{StaticResource ButtonFontFamily}" FontSize="{StaticResource ButtonFontSize}" FontWeight="{StaticResource ButtonFontWeight}"/> </StackPanel> </Window>
使用setter对(property, value)的方式定义资源,再通过使用样式来使用:
<Window.Resources> <Style x:Key="BigFontButtonStyle"> <Setter Property="Control.FontFamily" Value="Times New Roman"/> <Setter Property="Control.FontSize" Value="18"/> <Setter Property="Control.FontWeight" Value="Bold"/> </Style> </Window.Resources> <StackPanel VerticalAlignment="Center"> <Button Name="cmd" Content="Resource Button" Width="150" Height="30" Margin="3" Style="{StaticResource BigFontButtonStyle}"/> </StackPanel>
WPF的样式很强大,甚至可以包括事件,如:
<Window.Resources> <Style x:Key="MouseOverHighlightStyle" TargetType="TextBlock"> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="TextAlignment" Value="Center"/> <Setter Property="Padding" Value="5"/> <EventSetter Event="TextBlock.MouseEnter" Handler="element_MouseEnter"/> <EventSetter Event="TextBlock.MouseLeave" Handler="element_MouseLeave"/> </Style> </Window.Resources> <StackPanel VerticalAlignment="Center"> <TextBlock Text="This is a TextBlock" Style="{StaticResource MouseOverHighlightStyle}"/> </StackPanel>
后台代码
private void element_MouseEnter(object sender,MouseEventArgs e) { ((TextBlock)sender).Background = new SolidColorBrush(Colors.Aqua); } private void element_MouseLeave(object sender, MouseEventArgs e) { ((TextBlock)sender).Background = null; }
样式支持多层样式——样式的继承,比较好理解,不再细表。
T2所述的方法在很多时候仍然不够高效,比如有20个按钮且分布在不同的窗体中,需要统一的风格就会比较麻烦。WPF提供了一种全局的针对某个控件类型的样式设置,格式为:<Style x:Key="myButtonStyle" TargetType="Button">。如果不专门指定,则会使用 myButtonStyle 样式应用到作用域内的所有按钮。
<Window.Resources> <Style x:Key="MouseOverHighlightStyle" TargetType="TextBlock"> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="TextAlignment" Value="Center"/> <Setter Property="Padding" Value="5"/> <EventSetter Event="TextBlock.MouseEnter" Handler="element_MouseEnter"/> <EventSetter Event="TextBlock.MouseLeave" Handler="element_MouseLeave"/> </Style> <Style TargetType="TextBlock" BasedOn="{StaticResource MouseOverHighlightStyle}"> <Setter Property="Control.Foreground" Value="Green"/> </Style> </Window.Resources> <StackPanel VerticalAlignment="Center"> <TextBlock Text="This is a TextBlock" Style="{StaticResource MouseOverHighlightStyle}"/> <TextBlock Text="Inherited Style TextBlock" Style="{StaticResource BaseOnStyle}"/> <TextBlock Text="Global Style TextBlock"/> <TextBlock Text="Global Without Style TextBlock" Style="{x:Null}"/> </StackPanel>
通过这几个例子,可以满足基本的需求,再组合触发器可以实现更为丰富的功能。
资源的使用通过先定义再使用的方式。一般格式为 <对象.Resources> 中定义,其中对象为容器如Window,Grid等。在使用时格式为:FontFamily="{StaticResource ButtonFontFamily}",其中引用为静态和动态引用两种。区别在于是否会随着资源的修改而变化:静态的不会变化,而动态的会变化。所以当使用资源定义样式时,只要修改样式,可以通过动态引用资源的方式,实现统一更改效果的方式。
后台可以通过以下代码设置:
cmd.Style = (Style)cmd.FindResource("BigFontButtonStyle");
参考阅读:
参考资料:深入浅出WPF笔记——X名称空间详解, WPF学习之X名称空间详解。
x:Name
控件的访问名称,即控件的Name属性。
x:Class
用于告诉XAML编译器当前类与和后台哪个类合并,使用x:Class有以下几点要求:
x:FieldModifier
指示字段的公有属性,值包括 public, private, internal, protected。
x:Key
用于资源访问所使用的名称。
x:Type
用于指定资源的类型,如 UserWindowType="{x:Type local:Window1}"。
x:Shared
表示资源的共享类型,与x:Key配合使用。
当 x:Shared="true" 时,通过x:Key 返回的为同一个对象。
当 x:Shared="false" 时,通过x:Key 返回的为此对象的新副本。
x:Null
即 null 空值。
x:ClassModiffier
类的访问级别,值包括 public, private, internal。
x:Array
用于指定列表,如:
<x:Array Type="sys:String">
<sys:String>Jim</sys:String>
<sys:String>Darren</sys:String>
<sys:String>Frank</sys:String>
</x:Array>
x:static
指定使用的数据为static。如先在类内定义:public static string s1 = "Hello";,然后再在<Button Content="{x:Static local:Window4.s1}" Height="23" Name="button3""/>中访问。
x:Code
直接在XAML中添加C#代码,如:
<TextBlock MouseEnter="TextBlock_MouseEnter"> <x:Code> <![CDATA[ private void TextBlock_MouseEnter(object sender, MouseEventArgs e) { MessageBox.Show("test"); } ]]> </x:Code> </TextBlock>
2021/12/17 测试可以实现与 WinForm类似的 Splitter 的效果
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="3"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Button"></Button>
<GridSplitter Grid.Row="1" HorizontalAlignment="Stretch"></GridSplitter>
<Button Grid.Row="2" Content="Button"></Button>
</Grid>
<Menu Grid.Row="0" Background="#dddddd" FontFamily="黑体" FontSize="14"> <MenuItem Header="文件"> <MenuItem Header="分店信息设置"> </MenuItem> <MenuItem Header="综合信息设置"> </MenuItem> <Separator></Separator> <MenuItem Header="商品类别设置"> </MenuItem> <MenuItem Header="商品资料"> </MenuItem> <MenuItem Header="商品售价维护"> </MenuItem> <Separator></Separator> <MenuItem Header="客户类别设置"> </MenuItem> <MenuItem Header="客户资料"> </MenuItem> <Separator></Separator> <MenuItem Header="供应商类别设置"> </MenuItem> <MenuItem Header="供应商资料"> </MenuItem> <Separator></Separator> <MenuItem Header="银行账号设置"> </MenuItem> <MenuItem Header="部门人员设置"> </MenuItem> <MenuItem Header="快递设置"> </MenuItem> <MenuItem Header="数据导入"> </MenuItem> </MenuItem> </Menu>