WPF How to use Resource Dictionary in C#



To know what is Resource Dictionary,example of Resource dictionary, how we can add Resource Dictionary and how we can use it from XAML visit here:

Now there may be a case where we need to access a resource dictionary that we have defined in our project, from C# code.
If we have already merged our resource dictionary in XAML, it is easy to access the inner resources using thecontrol.FindResource("<<ResourceKey>>"); method.
 But, if we haven’t merged the resources in XAML and we still need to use the resource dictionary, we have options to use it directly inC# code. 
We have modified the example given here WPF How to use Resource Dictionary in XAML. where we were using ResourceDictionary from XAML.
Below is the XAML and code snippet:
XAML Code:
<UserControl x:Class="WPF_Sample_Project_Test.ResourceDictionaryExample"
             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">
        <Grid>
        <Button x:Name="btnRound"  />
    </Grid> 
</UserControl>
C# Code:
public partial class ResourceDictionaryExample : UserControl
    {
        private ResourceDictionary _myResourceDictionary;
        public ResourceDictionaryExample()
        {
            InitializeComponent();
            LoadResourceDictionary();
            ApplyStyle();
        }
        public void LoadResourceDictionary()
        {         
            _myResourceDictionary = new ResourceDictionary();
            _myResourceDictionary.Source =
                new Uri("/WPF Sample Project Test;component/Resources/MyResourceDictionary.xaml",
                    UriKind.RelativeOrAbsolute);   
        }
        public void ApplyStyle()
        {
            Style mybuttonstyle = _myResourceDictionary["RoundButtonStyle"] as Style;
            btnRound.Style = mybuttonstyle;
        }
    }

We have used a URI to hold the Resource Dictionary content. When we define the URI, the project name comes first and then the relative path. The UriKind option is very important. If we don’t mention the UriKind, it will not be able to find the resource.
As it is a resource dictionary, we can access styles by their keys. In the above example we have used key “RoundButtonStyle” to access the Button Style.

Note: The output will be same whether we use Resource Dictionary From XAML or from C#.

1 comment:

  1. Hub Of Computer Tricks And Programming: Wpf How To Use Resource Dictionary In C >>>>> Download Now

    >>>>> Download Full

    Hub Of Computer Tricks And Programming: Wpf How To Use Resource Dictionary In C >>>>> Download LINK

    >>>>> Download Now

    Hub Of Computer Tricks And Programming: Wpf How To Use Resource Dictionary In C >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete