在资源字典的DataTemplates不从的App.xaml风格继承?
-
12-09-2019 - |
题
我添加自定义命名样式到App.xaml中。
我创建了一个外部的资源字典(这是我在App.xaml中的合并词典附上),当我尝试使用在rcource字典上述命名样式之一,它说,没有这样的风格。
另外,默认的样式(即适用于整个申请即无名样式)不要在模板元素适用。
注:模板的生成操作 '页面'的
。下面是我的代码是如何编写的示例:
<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>
任何想法? 我必须以不同的方式合并词典?
解决方案
该代码将无法正常工作,因为DataTemplate中的资源字典不知道哪一个是使用它,它只是已使用。像好莱坞模式。它们单独编译。
要完成这项工作,你可以把你的风格,在DataTemplate中的相同资源字典的App.xaml或者,如果你不喜欢这种耦合,你可以把它放在不同的资源字典,它合并到DataTemplate中的资源字典强>
其他提示
您的App.xaml生成操作应该是ApplicationDefinition,并为您的资源字典文件的生成动作要页。我猜你有两个那些正确的,因为他们是默认的(我看到你提到的有关页面的话)。
我不认为你的情况的任何其他问题。只要你的静态资源在正确的顺序,他们似乎是定义,它应该能够找到他们,当你运行你的应用程序。
修改强>
调试想法:创建一个名为“TestDictionary.xaml”用一个简单的按钮样式新鲜的资源字典。确保这本词典是在同一文件夹作为其他字典(DataTemplate1.xaml等)。把一个链接,只在TestDictionary MergedDictionaries
(注释掉其他)。把你的启动窗口上的按钮和应用的样式。看看是否只是工作。如果失败了,你知道你有一个问题与您的合并。如果成功的话,讲讲你的DataTemplate
可能是问题所在。