scope resources to ResourceDictionary file

essamce 621 Reputation points
2020-05-06T00:03:43.943+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. mister.nobody 101 Reputation points
    2020-05-06T17:09:00.747+00:00

    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!

    1 person found this answer helpful.
    0 comments No comments