How can I make a 'Private' ResourceDictionary? [duplicate]
Possible Duplicate:
How to make a Style that only exists within the context of a ResourceDictionary
I'm building a complex ResourceDictionary which will expose ControlTemplates I want to share with projects that include that ResourceDictionary. However, this ResourceDictionary contains a series of supporting styles and templates which I don't want available to the projects. How can I do this?
For example:
<ResourceDictionary>
<!-- Private dictionary items used to set up the publicly usable Omnibox templates -->
<ResourceDictionary x:Key="PrivateDictionary">
<Thickness x:Key="BaseValueMarginAdjustment">2,0,0,0</Thickness>
<!--Base Styles -->
<Style x:Key="BaseElement" TargetType="FrameworkElement">...</Style>
<Style x:Key="GridStyle" TargetType="Grid" BasedOn="{StaticResource BaseElement}">...</Style>
<Style TargetType="Selector" x:Key="SelectorStyle" BasedOn="{StaticResource BaseElement}">...</Style>
...
</ResourceDictionary>
<!--Public CONTROL TEMPLATES -->
<ControlTemplate TargetType="{x:Type local:OmniBox}" x:Key="OBListBoxTemplate">
<Grid x:Name="PART_Grid" Style="{StaticResource GridStyle}">
<ListBox x:Name="PART_Value" Style="{StaticResource SelectorStyle}" />
...
</ControlTemplate>
<C开发者_如何学GoontrolTemplate ...>
...
</ResourceDictionary>
Note that the above does not work. In particular, the above compiles, but has a runtime error because the ControlTemplates I want publically visible can't find the above private styles like "GridStyle".
Tried the following without success as well:
<ResourceDictionary>
<!-- Private dictionary items used to set up the publicly usable Omnibox templates -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>...</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<!--CONTROL TEMPLATES -->
<ControlTemplate TargetType="{x:Type local:OmniBox}" x:Key="OBTextBoxTemplate">
...
Why can't you place these Resources for Styles in separate Resource Dictionary? So, across project where you don't want these Resources to be visible, simply don't merge them there.
Suppose you have Resource define in your App.xaml like this -
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary1.xaml"/>
<ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary2.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Suppose your Style Resources exist in Dictionary2.xaml, simply omit the second dictionary from it.
Edit:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfApplication1;component/Themes/Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
You can add the reference of ResourceDictionary1 in your ResourceDictionary2. And wherever you want the reosurces to be used say any UserControl you can always add reference of ResourceDictionary1 there in case you dont want the global reference.
精彩评论