Kir*_*rst 5 wpf xaml user-controls resourcedictionary
我有一个WPF UserControls库和一个在库中共享的ResourceDictionary.
此库中的所有UserControl仅出现在单个"shell"父控件中,该控件实际上只是一组较小控件的容器.当我添加以下XAML时,我能够按预期从我的shell控件访问ResourceDictionary
<Control.Resources>
<ResourceDictionary Source="MyResources.xaml" />
</Control.Resources>
Run Code Online (Sandbox Code Playgroud)
但是,我无法从位于"shell"控件内的子控件访问ResourceDictionary.
我的印象是WPF应该在本地检查资源,然后向上遍历,直到找到适当的资源?
相反,我得到了
Cannot find resource named '{BoolInverterConverter}'.
Resource names are case sensitive. Error at
object 'System.Windows.Data.Binding' in markup file...
Run Code Online (Sandbox Code Playgroud)
显然,我可以(并且)在我的子控件中引用ResourceDictionary; 但现在每个控件都需要引用这个字典,我相信这不是必需的.
任何想法,我做的事情是奇怪的还是我对行为的期望不正确?
这里描述的是什么,虽然文档有点不透明.如果在未指定键ResourceDictionary
的Resources
情况下向元素的属性添加,则WPF期望您合并资源字典,并使用其MergedDictionaries
属性中的字典内容填充字典.它忽略了ResourceDictionary
没有键的实际内容.
所以你想要做的是:
<Control.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Control.Resources>
Run Code Online (Sandbox Code Playgroud)
编辑:
一个工作的例子:
MainWindow.xaml:
<Window x:Class="MergedDictionariesDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:MergedDictionariesDemo="clr-namespace:MergedDictionariesDemo" Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<MergedDictionariesDemo:UserControl1 />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="UCBrush"
Color="Bisque" />
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
UserControl1.xaml:
<UserControl x:Class="MergedDictionariesDemo.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Border Margin="10" BorderBrush="Navy" BorderThickness="1" CornerRadius="10">
<TextBlock Margin="10"
Background="{DynamicResource UCBrush}">
The background of this is set by the resource UCBrush.
</TextBlock>
</Border>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5932 次 |
最近记录: |