Wpf 如何在自定义控件库中使用资源字典?
我用一些常用的笔刷创建了一个资源字典Wpf 如何在自定义控件库中使用资源字典?,wpf,dictionary,dll,custom-controls,resourcedictionary,Wpf,Dictionary,Dll,Custom Controls,Resourcedictionary,我用一些常用的笔刷创建了一个资源字典 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="GrayColor1&
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="GrayColor1" Color="#f2f2f2"/>
<SolidColorBrush x:Key="GrayColor2" Color="#e5e5e5"/>
<SolidColorBrush x:Key="GrayColor3" Color="#d9d9d9"/>
...
</ResourceDictionary>
...
我想在自定义控件库中的许多控件中使用它们,但我没有找到任何方法使它们对控件可用。在普通应用程序中,我会将它们放在app.xaml中,但在库中没有app.xaml文件。
那么在图书馆里使用资源词典的方法是什么呢 我已经尝试将字典合并到/Themes/Generic.xaml中,但没有成功,如下所示:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyControls;component/DefaultBrushes.xaml"/>
<ResourceDictionary Source="/MyControls;component/Styles/CustButton.xaml"/>
<ResourceDictionary Source="/MyControls;component/Styles/CustTextBox.xaml"/>
...
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
...
但是引用资源会导致空引用(似乎在Generic.xaml中只能合并控件模板)。您仍然可以在应用程序App.xaml中创建合并的词典,但在要访问这些笔刷的控件库中,请尝试使用DynamicSource而不是StaticResource
Background="{DynamicResource GrayColor1}"
您仍然可以在applications App.xaml中创建合并词典,但在要访问这些笔刷的控件库中,请尝试使用DynamicSource而不是StaticResource
Background="{DynamicResource GrayColor1}"
必须将它们合并到每个控件中,如果有嵌套控件,则必须合并到最顶层的控件中
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://Application:,,,/MyControls;component/Styles/CusTextBox.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
必须将它们合并到每个控件中,如果有嵌套控件,则必须将它们合并到最顶层的控件中
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://Application:,,,/MyControls;component/Styles/CusTextBox.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>