How can I access ResourceDictionary in wpf from C# code?

后端 未结 9 1588
[愿得一人]
[愿得一人] 2020-11-30 03:04

I have a DataTemplate defined in a xaml file that I want to access via C# code. Can anyone please tell me how can I access it? I added a new ResourceDicti

9条回答
  •  我在风中等你
    2020-11-30 03:38

    You can access a resource dictionary you added to your project as follows:

    var rd = new ResourceDictionary();
    rd.Source = new Uri("ms-appx:///Dictionary1.xaml");
    

    Then you can access a resource stored in the resource dictionary like so:

    someObject.Property = rd["mytemplate"];
    

    NOTE:
    You will have to modify the URI to the resource dictionary according to the location you created it relative to the project's base directory.

提交回复
热议问题