1,应用图标添加:
2,设置无Style风格的窗口
<WindowChrome.WindowChrome> <WindowChrome ResizeBorderThickness="{Binding ResizeBorderThickness}" GlassFrameThickness="0" CornerRadius="0" CaptionHeight="{Binding TitleHeight}" /> </WindowChrome.WindowChrome>
- ResizeBorderThickness---为窗口进行拖拉时候的厚度.
- GlassFrameThickness为窗口的边界
- CaptionHeight为窗口进行放大缩小之用.
3,添加资源字典和融合字典
App中添加资源:在App.xaml中进行添加
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Styles/Colors.xaml"/> <ResourceDictionary Source="/Styles/Fonts.xaml"/> <ResourceDictionary Source="/Styles/Texts.xaml"/> <ResourceDictionary Source="/Styles/Buttons.xaml"/> <ResourceDictionary Source="/Styles/Windows.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
自己添加资源
- 创建文件夹和资源文件
- 创建字体资源
<FontFamily x:Key="LatoThin">pack://application;,,,/Fonts/#Lato Thin</FontFamily> <FontFamily x:Key="LatoRegular">pack://application;,,,/Fonts/#Lato Regular</FontFamily> <FontFamily x:Key="LatoBold">pack://application;,,,/Fonts/#Lato Black</FontFamily> <FontFamily x:Key="FontAwesome">pack://application;,,,/Fonts/#FontAwesome</FontFamily> <Style TargetType="{x:Type Control}" x:Key="BaseStyle"> <Setter Property="FontFamily" Value="{StaticResource LatoThin}" /> <Setter Property="FontSize" Value="{StaticResource FontSizeRegular}" /> </Style> <Style TargetType="{x:Type TextBlock}" x:Key="BaseTextBlockStyle"> <Setter Property="FontFamily" Value="{StaticResource LatoThin}" /> <Setter Property="FontSize" Value="{StaticResource FontSizeRegular}" /> </Style> <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}" /> <Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseStyle}" /> <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}" /> <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseTextBlockStyle}" /> <Style TargetType="{x:Type ListView}" BasedOn="{StaticResource BaseStyle}" /> <system:Double x:Key="FontSizeSmaller">10</system:Double> <system:Double x:Key="FontSizeSmall">12</system:Double> <system:Double x:Key="FontSizeRegular">14</system:Double> <system:Double x:Key="FontSizeLarge">20</system:Double> <system:Double x:Key="FontSizeXLarge">24</system:Double> <system:Double x:Key="FontSizeXXLarge">30</system:Double>
注意:
- 字体名称和实际文件中字体名称的区别以及 pack://application;,,,/Folders…这个标准的查询资源文件的方式.
需要设置程序集和文件名
4,添加字典资源失败和成功的方式.
5,在添加Fonts时 Text Block和 Controls 不同的区别.
- 图个使Control一个是TextBlock
6,WPF更改命名空间后,找不到InitializeComponent()方法:删除原有的xaml ,重新新建一个.
<Window x:Class="Fasetto.Word.MainWindow"//更改这个地方吧.
7,WindowChrom的属性和说明
- ResizeBorderThickness---为窗口进行拖拉时候的厚度.
- GlassFrameThickness为窗口的边界
- CaptionHeight为窗口进行放大缩小之用.
-----------利用Resizer进行窗口重新定义:
- 获取鼠标当前位置(WPF)
首先导入dll:
[DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetCursorPos(out POINT lpPoint); [DllImport("user32.dll")]
其次建立POINT (和 Point不一样)struct----注意.StructLayout表明这是非优化块.建立的是顺序排布地址.在通讯等领域比较关键.
[StructLayout(LayoutKind.Sequential)] public struct POINT { /// <summary> /// x coordinate of point. /// </summary> #pragma warning disable IDE1006 // Naming Styles public int X; #pragma warning restore IDE1006 // Naming Styles /// <summary> /// y coordinate of point. /// </summary> #pragma warning disable IDE1006 // Naming Styles public int Y; #pragma warning restore IDE1006 // Naming Styles /// <summary> /// Construct a point of coordinates (x,y). /// </summary> public POINT(int x, int y) { X = x; Y = y; } public override string ToString() { return $"{X} {Y}"; } } #endregion
public Point GetCursorPosition() { // Get mouse position GetCursorPos(out var lMousePosition); // Apply DPI scaling return new Point(lMousePosition.X / mMonitorDpi.Value.DpiScaleX, lMousePosition.Y / mMonitorDpi.Value.DpiScaleY); }
详细信息会在详细解析Resize类里面进行讲解.
8,OpacityMask and VisualBrush ViewBox .
OpacityMask 用于使用Brush进行设定透明度.也就是对应的画刷在对应的点上如果是透明的,则该元素也是透明的
VisualBrush 主要是使用Visual类作为Brush的源.
9,快捷:如何快捷跳到行尾.如何快速选择属性.
可以在选项--->键盘--->编辑---行尾,设置快捷键ctrl+;
10:MarkupExtension:用于扩展标记,使标记不仅支持字符串也支持别的.
https://www.cnblogs.com/Code-life/p/3463362.html
<Window x:Class="WPFSample.Samples.MarkupExtensionSample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:me="clr-namespace:WPFSample.Samples.MarkupExtensionSamples" Title="MarkupExtensionSample" Height="300" Width="300"> <Grid> <StackPanel> <TextBlock Foreground="Aqua" Text="Foreground"/> <!--Following xaml code set a foreground using markup extension.--> <TextBlock Foreground="{me:BrushGetter TitleBrush}" Text="Foreground from markup extension"/> <TextBlock Foreground="{me:BrushGetter ContentBrush}" Text="Foreground from markup extension"/> </StackPanel> </Grid> </Window>
public class BrushGetter : MarkupExtension { private string _brushName; public BrushGetter(string brushName) { //在Xaml中使用该MarkupExtension时传递brushName参数。 _brushName = brushName; } public override object ProvideValue(IServiceProvider serviceProvider) { //当在通过该MarkupExtension在Xaml中给属性赋值时,该方法将被调用。根据条件返回一个合适的对象即可。 //至于参数serviceProvider,一般情况下用不到。暂时不纠结它了。 switch (_brushName) { case "TitleBrush": return Brushes.Black; case "ContentBrush": return Brushes.Blue; default: break; } return null; } }
11,如何在XAML中创建自定义的XAML集合对象:
internal class Student { public string Name { get; set; } public int Age { get; set; } } internal class StudentList:List<Student> { } class StringCollect { public StudentList Students { get; set; } }
internal class Student { public string Name { get; set; } public int Age { get; set; } } internal class StudentList:List<Student> { } class StringCollect { public StudentList Students { get; set; } }
<Window.DataContext> <local:StringCollect x:Name="c2" > <local:StringCollect.Students> <local:StudentList> <local:Student Age="18" Name="A1"/> <local:Student Age="18" Name="A2"/> <local:Student Age="18" Name="A3"/> </local:StudentList> </local:StringCollect.Students> </local:StringCollect> </Window.DataContext> <Grid> <ListBox ItemsSource="{Binding ElementName=c2,Path=Students}"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBlock> <Run Text="Name:"/> <Run Text="{Binding Name}"/> </TextBlock> <TextBlock Grid.Column="1"> <Run Text="Age:"/> <Run Text="{Binding Age}"/> </TextBlock> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
总之,可以将某个泛型的类型,再自定义一个继承的类型然后就可以了.或者使用X:Array方法:
<ListBox > <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBlock> <Run Text="Name:"/> <Run Text="{Binding Name}"/> </TextBlock> <TextBlock Grid.Column="1"> <Run Text="Age:"/> <Run Text="{Binding Age}"/> </TextBlock> </Grid> </DataTemplate> </ListBox.ItemTemplate> <ListBox.ItemsSource> <x:Array Type="{x:Type local:Student}"> <local:Student Age="18" Name="b1"/> <local:Student Age="18" Name="b2"/> <local:Student Age="18" Name="b3"/> </x:Array> </ListBox.ItemsSource> </ListBox>
12,在Element.Style中设置属性和在Element本体中设置属性的区别
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBoxBase}"> <Grid> <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/> </Border> <TextBlock IsHitTestVisible="False" Text="{TemplateBinding Tag}" x:Name="placeholder" FontFamily="{StaticResource LatoThin}" Padding="{TemplateBinding Padding}" VerticalAlignment="Center" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Foreground="{StaticResource ForegroundDarkBrush}" > <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding Text,RelativeSource={RelativeSource TemplatedParent}}" Value=""> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" TargetName="border" Value="0.56"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/> </Trigger> <Trigger Property="IsKeyboardFocused" Value="True"> <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>
这个代码中的Visibility属性的区别: 必须放在Style里面,否则的话,其不会显示.因为依赖性属性的原因,后面的会覆盖前面的.
1,Visibility=Collapsed 必须放在style里面,否则会被覆盖.
2,
> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Mode=TemplatedParent}}" Value=""> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock>
在这里,的Binding的一个用发,其绑定了父模板的Text属性.用其他的写法较难实现.{TemplateBinding 也不行}
13,TemplateBinding 和Binding之间的区别:
TemplateBinding与Binding区别
(1)TemplateBinding只是单方向的数据绑定
(2)TemplateBinding不会自动转换数据类型
下面两个绑定效果是一样的
<TextBlock Text="{TemplateBinding MyText}"/>
<TextBlock Text="{Binding Path=MyText, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
RelativeSource={RelativeSource TemplatedParent}——
TemplateBinding是连接控件和模板中属性的一种绑定,从自定义控件的属性中提取属数据。
优点:轻量,开销比较小。
缺点:只能单向绑定,不能绑定到继承Freezable的类上。