How to get the resource key from a control?

William Liu 466 Reputation points
2024-09-10T02:55:22.18+00:00

How to get the resource key from a control?

Wpf control can use the pre-defined resource in runtime. Is it able to get the resource key from a control? And how?

Thanks!

<Window
    x:Class="GetResourceKeyWpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=System.Runtime"
    Width="800"
    Height="450"
    WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <system:String x:Key="MyName">Will</system:String>
        <system:String x:Key="MyAddress">Beijing</system:String>
    </Window.Resources>
    <StackPanel>
        <TextBlock Loaded="MyNameTb_Loaded" Text="{StaticResource MyName}" />
        <TextBlock Loaded="MyAddressTb_Loaded" Text="{DynamicResource MyAddress}" />
    </StackPanel>
</Window>
using System.Windows;

namespace GetResourceKeyWpf;

public sealed partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void MyNameTb_Loaded(object sender, RoutedEventArgs e)
    {
        // Is it able to the static resource key "MyName" for text property?
    }

    private void MyAddressTb_Loaded(object sender, RoutedEventArgs e)
    {
        // Is it able to the dynamic resource key "MyAddress" text property?
    }
}
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,778 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 1,995 Reputation points Microsoft Vendor
    2024-09-10T06:40:52.3+00:00

    Hi,@William Liu. Welcome to Microsoft Q&A. 

    In WPF, you could get all x:Key in Resources through this.Resources.Keys.

    The reference code is as follows.

    <Window.Resources>
        <system:String x:Key="MyName">Will</system:String>
        <system:String x:Key="MyAddress">Beijing</system:String>
    </Window.Resources>
    <StackPanel>
        <TextBlock x:Name="Text1" Loaded="MyNameTb_Loaded"/>
        <TextBlock x:Name="Text2" Loaded="MyAddressTb_Loaded"/>
    </StackPanel>
    
        public partial class MainWindow : Window
        {
            List<string> keys = new List<string>();
            public MainWindow()
            {
                InitializeComponent();        
                foreach (string key in this.Resources.Keys) { 
                    keys.Add(key);
                }
            }
    
            private void MyNameTb_Loaded(object sender, RoutedEventArgs e)
            {
                Text1.Text = keys[0];
            }
    
            private void MyAddressTb_Loaded(object sender, RoutedEventArgs e)
            {
                Text2.Text = keys[1];
            }
        }
    

    If you want to use key-value data types in XAML, you could try using DictionaryEntry. Here is how to use it.

    xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
    
    <Window.Resources>
        <collections:DictionaryEntry x:Key="MyDictionaryEntry" Key=" MyName " Value=" Will "></collections:DictionaryEntry>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="{Binding Path=Key,Source={StaticResource MyDictionaryEntry}}"></TextBlock>
        <TextBlock Text="{Binding Path=Value,Source={StaticResource MyDictionaryEntry}}"></TextBlock>
    </StackPanel>
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.