《深入浅出WPF》笔记四

1、WPF资源分布:数据库、资源文件、WPF对象资源、变量
2、每个WPF的界面都具有一个名为Resources的属性,其类型为ResourceDictionary,以键值对的形式存储资源。
3、检索资源时,控件先查找自己的Resources属性,如果没有,程序会沿LogicalTree向上一级控件查找。如果最顶层容器也没有,就会查找Application.Resources。
4、在C#代码中使用定义在XAML代码里的资源:

string text=(string)this.FindResource("str");

如果明确知道资源存在:

string text=(string)this.Resources["str"];

5、使用XAML资源文件添加程序皮肤
把相应的XAML文件添加进项目中,然后用Source属性进行引用。

<Window.Resources>
    <ResourceDictionary Source="ShinyRed.xaml"/>
</Window.Resources>

6、静态资源与动态资源
静态资源:程序载入内存时对资源的一次性使用,之后不再访问该资源。也就是说初始化之后不再改变。
动态资源:程序运行过程中仍然会访问该资源。也就是说程序运行过程中资源可能会改变。
举例:如果程序运行过程中允许用户更改程序皮肤的配色方案,则用动态资源,否则用静态资源。
7、对象资源与二进制资源
对象资源:ResourceDictionary中的资源
二进制资源:应用程序的内嵌资源(图标、图片、文本、音频、视频等等)
8、向程序添加二进制资源
(1)添加的资源为字符串
在项目管理器中展开Properties结点,双击Resources.resx。编译后,可以使用Properties命名空间中的Resources类的方法或属性去获取该文件中的资源。
把Resources.resx的访问级别有Internal改为Public,这样XAML编译器才能访问这个类。
在XAML使用Resources.resx中的资源。
先把Properties中的名称空间映射为XAML名称空间

xmlns:prop="clr-namespace:DisplayWatch.Properties"

然后用x:Static标签扩展来访问资源:

<TextBlock Text="{x:Static prop:Resources.UserName}"/>

在C#中使用资源

this.textBlock.Text=Properties.Resources.Password;

(2)添加的资源是图标、图片、音频或视频等
在项目管理器的适当文件夹中右键:Add-->ExistingItems。
如果想让外部文件编译成二进制资源,则把BuildAction属性值设为Resources,Copy to Output Dictionary设为Do Not Copy。否则,把BuildAction属性值设为None,Copy to Output Dictionary设为CopyAlways。
9、使用Pack URI访问二进制资源
使用相对路径

<Image x:Name="ImagBg" Source="Resources/Image/Rafale.jpg" Stretch="Fill"/>

this.ImagBg.Source=new BitmapImage(new Uri(@"Resources/Image/Rafale.jpg",UriKind.Relative));

使用绝对路径

<Image x:Name="ImagBg" Source="pack://application:,,,/Resources/Image/Rafale.jpg" Stretch="Fill"/>

this.ImagBg.Source=new BitmapImage(new Uri(@"pack://application:,,,/Resources/Image/Rafale.jpg",UriKind.Absolute));

10、控件的内容
算法:控件的功能(响应的操作、激发的事件等)
数据:控件要展示的具体数据
11、WPF中的Template
ControlTemplate:算法内容的表现形式
DataTemplate:数据内容的表现形式
12、使用DataTemplate代替UserControl,使用数据驱动代替事件驱动
(1)创建数据模型

public class Car
    {
        public string Automaker { get; set; }
        public string Name { get; set; }
        public string Year { get; set; }
        public string TopSpeed { get; set; }
    }

(2)在Window.Resources中添加DataTemplate资源

<Window.Resources>
        <DataTemplate x:Key="listTemplate">
            <Border BorderBrush="Black" BorderThickness="2" CornerRadius="6">
                <StackPanel Margin="5" >
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <TextBlock Text="Name:"/>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="Year:"/>
                        <TextBlock Text="{Binding Year}"/>
                    </StackPanel>
                </StackPanel>
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="detailTemplate">
            <Border BorderBrush="Black" BorderThickness="2" CornerRadius="6">
                <StackPanel Margin="5" >
                    <StackPanel  Margin="5">
                        <TextBlock Text="Name:"/>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="AutoMaker:"/>
                        <TextBlock Text="{Binding Automaker}"/>
                        <TextBlock Text="Year:"/>
                        <TextBlock Text="{Binding Year}"/>
                        <TextBlock Text="Top Speed:"/>
                        <TextBlock Text="{Binding TopSpeed}"/>
                    </StackPanel>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>

(3)对控件进行布局,并使用DataTemplate

<StackPanel Orientation="Horizontal" Margin="5">
        <UserControl ContentTemplate="{StaticResource detailTemplate}"
                     Content="{Binding SelectedItem,ElementName=listBoxCars}"/>
        <ListBox x:Name="listBoxCars" Width="300" Margin="5,0" ItemTemplate="{StaticResource listTemplate}"/>
    </StackPanel>

(4)在C#代码中添加数据资源

           List<Car> carlist = new List<Car>()
            {
                new Car(){Automaker="Lamboghini",Name="Diabo",Year="1990",TopSpeed="340"},
                new Car(){Automaker="Lamboghini",Name="Murcielago",Year="2001",TopSpeed="353"},
                new Car(){Automaker="Lamboghini",Name="Gallardo",Year="2003",TopSpeed="325"},
                new Car(){Automaker="Lamboghini",Name="Reventon",Year="2008",TopSpeed="356"},

            };
            this.listBoxCars.ItemsSource = carlist;

13、使用ControlTemplate改变控件的外观
(1)编辑某个控件的ControlTemplate(如TextBox)
首先,在界面中放入一个TextBox,然后选中,右键选择EditTemplate->Edit a copy,输入Style Key,选择资源保存的位置(如Application),确定。就会在XAML的Resources标签中出现控件的原始Style,ControlTempla
te就在里面,按照自己的要求对其修改即可。
(2)ItemsControl的PanelTemplate
比如将ListBox变为水平方向显示

        <ListBox>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>    

14、DataTemplate和ControlTemplate的关系
ControlTemplate决定控件外观,对应Template属性
DataTemplate决定数据外观,对应ContentTemplate属性
DataTempate控件树是ControlTemplate控件树的子树
15、DataTemplate和ControlTemplate的应用
(1)把ControlTemplate应用在所有目标上
设置Style的TargetType

<Style TargetType="{x:Type TextVox}">

如果个别目标不需要应用该Style,则将该控件的Style标记为{x:Null}

<TextBox Style="{x:Null}"/>

(2)把DataTemplate应用在某个数据类型上
设置DataTemplate的DataType属性

<DataTemplate DataType="{x:Type local:Car}">
</DataTemplate>

(3)将XML文件中的数据应用到DataTemplate中
数据绑定时,使用XPath指定相应的结点作为Binding中的Path。
15、显示层级数据
使用HierarchicalDataTemplate。

     <Window.Resources>
        <XmlDataProvider x:Key="ds" Source="Data.xml" XPath="Data/Grade"/>
        <HierarchicalDataTemplate DataType="Grade" ItemsSource="{Binding XPath=Grade}">
            <TextBlock Text="{Binding XPath=@Name}"/>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="Class" ItemsSource="{Binding XPath=Class}">
            <RadioButton Content="{Binding XPath=@Name}" GroupName="gn"/>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="Group" ItemsSource="{Binding XPath=Group}">
            <CheckBox Content="{Binding XPath=@Name}"/>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <TreeView ItemsSource="{Binding Source={StaticResource ds}}"/>
    </Grid>

16、使用DataTemplate来定制GridViewColumn的CellTemplate
注意,改变CellTemplate后,可能需要处理焦点问题。(比如TextBox和ListItem的关系)
17、Style中的两种重要属性:Setter和Trigger
18、Trigger的分类
Trigger 普通触发
MultiTrigger 多条件触发
DataTrigger 数据触发
MultiDataTrigger 多数据条件触发
EventTrigger 事件触发。多用来执行一段动画。 

关于绘图和动画

1、WPF绘图,常用的绘图容器:Canvas和Grid
2、WPF绘图元素
Line直线
Rectangle矩形
Ellipse椭圆
Path路径(最强大)
3、使用Path裁剪界面元素
使用窗体或控件的Clip界面属性
4、效果 Effect
BlurEffect 模糊效果
DropShadowEffect 投影效果
ShaderEffect 着色器效果(抽象),用于开发滤镜插件
从http://wpffx.codeplex.com可以下载官方的滤镜压缩包。
5、Transform
(1)RenderTransform:呈现变形
只改变元素“出现在哪里”,不牵扯布局的改变,只涉及窗体的重绘。用于制作动画的时候提高效率。
(2)LayoutTransform:布局变形
会影响窗体的布局。一般只用在静态变形上,不用于制作动画。
6、关键帧动画:方便协同多个动画,串行执行的一组动画
7、场景Storyboard:并行执行的一组动画

原文地址:https://www.cnblogs.com/tt2015-sz/p/4818180.html