我有一个DataTemplate
我正在尝试定义并在其中我有一个Button
我想设置的风格。目前我有UserControl
MergedDictionaries
...可能更好地显示一些代码:
<UserControl.Resources>
<ResourceDictionary.MergedDictionaries>
<!-- MyButtonStyle is in this dictionary -->
<ResourceDictionary Source="ms appx:///Dictionaries/ButtonStyles.xaml"/>
<ResourceDictionary>
<DataTemplate x:Key="MyDataTemplate">
<Grid>
<!-- Here is the button I want to apply the style to -->
<Button Style="{StaticResource MyButtonStyle}"/>
</Grid>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</UserControl.Resources>
我想将Button
内的DataTemplate
样式设置为位于MyButtonStyle
ButtonStyles.xaml
的{{1}}。但我收到以下错误:
Resource `MyButtonStyle` is not found
如何在我的示例中正确引用资源?
答案 0 :(得分:0)
StaticResource将按以下顺序搜索资源:
它不会搜索兄弟合并的词典。
在您的情况下,MyButtonStyle
不包含在MyDataTemplate
的ResourceDictionary中,也不包含在任何MergedDictionaries中。你需要的是:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MyButtonStyle is in this dictionary -->
<ResourceDictionary Source="ms appx:///Dictionaries/ButtonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="MyDataTemplate">
<Grid>
<!-- Here is the button I want to apply the style to -->
<Button Style="{StaticResource MyButtonStyle}"/>
</Grid>
</DataTemplate>
<ResourceDictionary>
</UserControl.Resources>
答案 1 :(得分:0)
@nevermind对命令的表述非常准确。但是,为了导入ResourceDictionary
,请使用以下路径
<ResourceDictionary Source="/MyAssemblyName;component/Folder1/Folder2/Res.xaml"></ResourceDictionary>