标题
郝伟 2021/0/0

简介

WPF UI元素类型
https://www.bbsmax.com/A/GBJr6EbBJ0/
https://www.cnblogs.com/xiongyang123/p/10682782.html

xmlns:local=“clr-namespace:WpfApplication2”

T 样式

这篇文章很好,测试可用: https://blog.csdn.net/qq_34802416/article/details/78231575

T1. 通过定义类型-值资源后直接设置

定义了类型-值的成对数据资源,然后再使用,具体步骤:

  1. 设置类型-值属性 (type, value)
  2. 在需要用的地方使用
    使用有点麻烦,因为需要为每个属性指定定义好的资源值。
<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>

T2. 通过定义属性-值资源后使用样式

使用setter对(property, value)的方式定义资源,再通过使用样式来使用:

  1. 设置属性-值属性 (type, value)
  2. 在需要用的地方通过style来使用
    使用相对简单,只要定义style即可。
<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;
}

样式支持多层样式——样式的继承,比较好理解,不再细表。

T3. 对指定控件类型进行样式设置

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>

通过这几个例子,可以满足基本的需求,再组合触发器可以实现更为丰富的功能。

T4. 静态资源(StaticResource)和动态资源(DynamicResource)

资源的使用通过先定义再使用的方式。一般格式为 <对象.Resources> 中定义,其中对象为容器如Window,Grid等。在使用时格式为:FontFamily="{StaticResource ButtonFontFamily}",其中引用为静态和动态引用两种。区别在于是否会随着资源的修改而变化:静态的不会变化,而动态的会变化。所以当使用资源定义样式时,只要修改样式,可以通过动态引用资源的方式,实现统一更改效果的方式。

后台可以通过以下代码设置:
cmd.Style = (Style)cmd.FindResource("BigFontButtonStyle");

参考阅读:

  1. https://www.cnblogs.com/zjoch/p/3647329.html
  2. https://blog.csdn.net/weixin_30892763/article/details/99394271

x命名空间

参考资料:深入浅出WPF笔记——X名称空间详解, WPF学习之X名称空间详解

<x:Array Type="sys:String">
    <sys:String>Jim</sys:String>
    <sys:String>Darren</sys:String>
    <sys:String>Frank</sys:String>
</x:Array>
<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>