question

essamce avatar image
essamce asked ·

scope resources to ResourceDictionary file

hi
is it possible to make some value (ex: Double) scoped to it's ResourceDictionary file so it wouldn't be accessible elsewhere.

thanks in advance

windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

misternobody avatar image
misternobody answered ·

I'm not aware of your specific use case, but one approach I used for sharing resources between styles is this:

PrivateResourceDictionary.xaml:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:local="clr-namespace:MyApplication.Styles">
    
     <!-- Common resources for Menu, ContextMenu and MenuItem styles -->
    
     <!-- Do not merge this resource dictionary into any other resource dictionary!
          Otherwise, the resources may become visible to other parts of the application!
          Only use the resource dictionary directly from within a style or control template! -->
    
     <!-- Border color of the menu popup -->
     <SolidColorBrush x:Key="MenuPopupBorderBrush" Color="#ABABAB"/>
    
 </ResourceDictionary>

OtherResourceDictionary.xaml:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:local="clr-namespace:MyApplication.Styles">
    
     <Style TargetType="{x:Type ContextMenu}">
    
         <Style.Resources>
             <ResourceDictionary>
    
                 <ResourceDictionary.MergedDictionaries>
    
                     <!-- Use resources from common resource dictionary -->
                     <ResourceDictionary Source="PrivateResourceDictionary.xaml"/>
    
                 </ResourceDictionary.MergedDictionaries>
    
             </ResourceDictionary>
         </Style.Resources>
    
         <!-- You can define your style here and use the merged resources -->
    
     </Style>
    
 </ResourceDictionary>

Just make sure that you merge your private resource dictionary only into the style itself but not into the using resource dictionary.

Hope this helps!

Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.