我是靠谱客的博主 细腻微笑,这篇文章主要介绍WPF中UserControl 绑定样式(小白教程)1.绑定xaml页内样式2.绑定样式资源(资源词典)文件中的样式,现在分享给大家,希望可以做个参考。

1.绑定xaml页内样式

这个操作只需要对需要更改样式的窗体内进行,不涉及别的页面。适合只需要更改一个窗体内的控件样式。

 这里的TargetType是为了绑定控件类型的,也就是当你引用这个样式后,窗体中与样式中控件类型相同的控件,均为引用这个样式。 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<UserControl x:Class="WPF登录.View.ManagerAddOperate" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPF登录.View" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.Resources> <Style TargetType="{x:Type TextBox}"> <Setter Property="Width" Value="120"/> <Setter Property="Height" Value="23"/> <Setter Property="FontSize" Value="25"/> <Setter Property="TextWrapping" Value="Wrap"/> <Setter Property="VerticalAlignment" Value="Top" /> <Setter Property="HorizontalAlignment" Value="Left"/> </Style> </UserControl.Resources> <Grid> <!--各种控件--> </Grid> </UserControl>

这里没有指定key值,那么这个窗体内所有TextBox控件都用此样式,不需要其他操作。但是,需要注意的是,如果在控件中还有样式中相同属性的不同设置,那么以你控件此时设置的属性为主,不在引用样式中的属性。

2.绑定样式资源(资源词典)文件中的样式

我把我需要用到的控件样式写到了一个资源词典(Dictionary)中。而资源词典专门被放在一个文件夹,程序中的窗体被放在另一个文件夹中。

1.创建样式文件

新建一个资源词典,后再其中写上你需要统一的样式(左边是代码,右边是我存放资源词典的位置)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WPF登录.Assets.Styles"> <Style TargetType="TextBox" x:Key="Input"> <Setter Property="Width" Value="120"/> <Setter Property="Height" Value="25"/> <Setter Property="FontSize" Value="25"/> <Setter Property="TextWrapping" Value="Wrap"/> <Setter Property="VerticalAlignment" Value="Top" /> <Setter Property="HorizontalAlignment" Value="Left"/> </Style> </ResourceDictionary>

2. 程序内绑定样式

进入到UserControl窗口中,添加资源文件,后再控件进行引用。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<UserControl x:Class="WPF登录.View.ManagerAddOperate" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPF登录.View" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="../Assets/styles/UserControl.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> <Grid> <TextBox x:Name="UserName" Style="{StaticResource Input}" /> </Grid> </UserControl>

 这里引用的样式名称就是我资源文件中的Key对应的名称。在窗体引用时Source的路径应为绝对路径

 参考文献:点击这里

最后

以上就是细腻微笑最近收集整理的关于WPF中UserControl 绑定样式(小白教程)1.绑定xaml页内样式2.绑定样式资源(资源词典)文件中的样式的全部内容,更多相关WPF中UserControl内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(110)

评论列表共有 0 条评论

立即
投稿
返回
顶部