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

后端 未结 9 1534
[愿得一人]
[愿得一人] 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:15

    I found the answer here

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/how-to-use-a-resourcedictionary-to-manage-localizable-string-resources

    • create a ressource dictionary "ColorResources.xaml"
    • add to it: Blue

    • edit your app.xml and add:

    • use the color from your code

      var color = (System.Windows.Media.Color)Application.Current.FindResource("ButtonColor1");

    and voilà

    ps : admin can you fix the code? it does not show up, thanks

    0 讨论(0)
  • 2020-11-30 03:18

    If you for example have a template for Button in your resource dictionary in the App.xaml file you can access it using the following code:

    Application.Current.Resources[typeof(Button)]
    
    0 讨论(0)
  • 2020-11-30 03:24

    Any of the above approaches work getting the resource based on the location, if you are following MVVMm I would recommend doing it this way:

    1. create a Service like ProvideDataTemplateService, (to create a service usual inherit from Behavior )
    2. Use Container of Your choice to inject this service where you would like to have aces to DataTemple.
    0 讨论(0)
  • 2020-11-30 03:25

    Where exactly are you defining it?

    If you define it in the ResourceDictionary of your object, then

    Application.Current.Resources[typeof(yourDataTemplateTargetType)] 
    

    should work. If you are defining it as a member of something else, like say, an ItemsControl, you need to get a handle to the ItemsControl instance and call the ItemTemplate property.

    Edit: Ok, I think we're getting somewhere. So you are defining a ResourceDictionary in its own file. Before you can use it in your UI and access it from your code behind, you need to merge that ResourceDictionary into your application. Are you doing this?

    If you are, then the next step is to get this resource. Each FrameworkElement has a method called FindResource. This method is great because it walks up the ResourceDictionary tree and attempts to locate the resource with the key. So, if you want to access this resource from a UserControl, you can do the following in the code behind:

    FindResource(typeof(yourDataTemplateTargetType));
    

    If this doesn't work for you, please show us exactly how you are declaring this resource dictionary and how it is getting merged into your application's resources.

    0 讨论(0)
  • 2020-11-30 03:30

    If you have merged resource dictionary using code like below

    <Window x:Class="MainWindow">
        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="DefaultStyle.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
    </Window>
    

    Then, instead of Application.Current.Resources["ResourceKey"] you need to specify Control name (in this case MainWindow) also like below

    var style = Application.Current.MainWindow.Resources["ResourceKey"];
    // OR
    var style = Application.Current.MainWindow.TryFindResource("ResourceKey");
    
    0 讨论(0)
  • 2020-11-30 03:31

    If you're getting the resources within the same project, try this:

    yourControl.Style = FindResource("YourResourceKey") as Style;
    

    Otherwise, try this:

    ResourceDictionary res = (ResourceDictionary)Application.LoadComponent(new Uri("/ProjectName;component/FolderName/ResourceDictionaryName.xaml", UriKind.Relative)); 
    yourControl.Style = (Style)res["YourResourceKey"];
    
    0 讨论(0)
提交回复
热议问题