【发布时间】:2023-03-16 22:56:02
【问题描述】:
我在 app.xaml 中添加了自定义命名样式。
我创建了一个外部资源字典(我附加在 app.xaml 的合并字典中),当我尝试在 rcource 字典中使用上述命名样式之一时,它说没有这样的样式。
此外,默认样式(即适用于整个应用程序的未命名样式)不适用于模板元素。
注意:模板的 Build Action 是 'Page'。
这是我的代码编写方式的示例:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<ResourceDictionary>
<Style
x:Key="StackPanelStyle"
TargetType="StackPanel"
BasedOn="{StaticResource {x:Type StackPanel}}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="Height" Value="40"/>
</Style>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Templates/DataTemplate1.xaml"/>
<ResourceDictionary Source="/Templates/DataTemplate2.xaml"/>
<ResourceDictionary Source="/Templates/DataTemplate3.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
这是一个数据模板的例子:
<DataTemplate DataType="{x:Type Entity}" x:Key="NameDataTemplate">
<Expander>
<StackPanel>
<--The following line produces: StackPanelStyle was not found.-->
<StackPanel Style="{StaticResource StackPanelStyle}">
<Label Content="Name:"/>
<TextBox Text="{Binding Name}"/>
</StackPanel>
</StackPanel>
</Expander>
</DataTemplate>
有什么想法吗? 我必须以不同的方式合并字典吗?
【问题讨论】:
-
只是出于好奇,
NameDataTemplate存储在哪个资源字典文件中? -
这只是一个例子,我的意思是强调样式在合并DataTemplates之前出现在app.xaml中。
标签: wpf styles resourcedictionary app.xaml