我目前正在创建一个自定义控件(基于WPF DataGrid).我想要做的是在datagrid上设置默认样式.目前我正在设置Style属性.但是当我创建一个改变fx的样式时,我的问题就出现了.主应用程序app.xaml中的背景颜色.然后,我的所有"默认"样式都将丢失,并且DataGrid仅在设置了background属性时才显示所有标准.
我已经尝试在网格上的每个属性上使用OverrideMetadata,我想要应用默认值但没有运气.我还尝试在构造函数中设置每个属性,但由于属性优先,因此从不应用主应用程序中的样式.
有任何想法吗?
提前致谢
Ken*_* K. 13
你在static构造函数中设置了吗?
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomType), new FrameworkPropertyMetadata(typeof(MyCustomType)));
Run Code Online (Sandbox Code Playgroud)
此外,您的资源样式的键是否等于您的自定义控件的类型?
即使将TargetType设置为您的控件,它也不能具有其他一些键集.
大会还应标有以下属性:
[assembly: ThemeInfo(
//where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.None,
//where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
ResourceDictionaryLocation.SourceAssembly
)]
Run Code Online (Sandbox Code Playgroud)
如果创建没有字典键的样式,它将为您在导入样式字典的范围内指定类型的所有对象设置样式(如果在Window.Resources中指定它,则将具有该Window的范围...如果在App.xaml中指定它,则会得到图片)。
<Style TargetType="{x:Type Button}">
<Setter Property="FontFamily" Value="Times New Roman" />
<Setter Property="FontSize" Value="30" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Background" Value="#FFCA5132" />
</Style>
Run Code Online (Sandbox Code Playgroud)
这将使它们具有相同的样式。
这是一个非常强大的功能。它允许您设置任何对象的样式,而不仅仅是UI元素。例如,您可以设置一个数据实体的样式,例如“ Person”对象,并且当可视地使用这些元素时(例如将Person类型的列表数据绑定到ListView时),即使它们是不是本机UI元素。这就是WPF如何对其控件“看不清”的方式。
| 归档时间: |
|
| 查看次数: |
8432 次 |
| 最近记录: |