How to get the static resource key from a control?

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

Thanks!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><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>
</Window.Resources>
<StackPanel>
<TextBlock Loaded="MyNameTb_Loaded" Text="{StaticResource MyName}" />
</StackPanel>
</Window>
</code>
<code><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> </Window.Resources> <StackPanel> <TextBlock Loaded="MyNameTb_Loaded" Text="{StaticResource MyName}" /> </StackPanel> </Window> </code>
<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>
    </Window.Resources>
    <StackPanel>
        <TextBlock Loaded="MyNameTb_Loaded" Text="{StaticResource MyName}" />
    </StackPanel>
</Window>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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?
}
}
</code>
<code>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? } } </code>
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?
    } 
}

I searched my question in SO, and got a similar question for get the key for dynamic resource.

How to get a dynamic resource key name programmatically?

I tried it for static resource but ReadLocalValue just returned the string value “Will” which is not my target.

PS:

My expected result is to get the string “MyName”, not the content “Will”.

Then, I can do some job like TextBlock.ToolTips = resourceKey, to display the key when my app is running in debug mode for checking if someone choosed the wrong resource.

10

Logically, the StaticResource Markup Extension is equivalent to the FindResource method.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>object myName = FindResource("MyName"); // return "Will"
</code>
<code>object myName = FindResource("MyName"); // return "Will" </code>
object myName = FindResource("MyName"); // return "Will"

If a certain hierarchy of resources is possible and you need to get a value from a certain level of the hierarchy, then you need to execute a method from an element from this level of the hierarchy.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <Window.Resources>
<system:String x:Key="MyName">Will</system:String>
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<system:String x:Key="MyName">Will-StackPanel</system:String>
</StackPanel.Resources>
<TextBlock x:Name="tbl" Loaded="MyNameTb_Loaded" Text="{StaticResource MyName}" />
</StackPanel>
</code>
<code> <Window.Resources> <system:String x:Key="MyName">Will</system:String> </Window.Resources> <StackPanel> <StackPanel.Resources> <system:String x:Key="MyName">Will-StackPanel</system:String> </StackPanel.Resources> <TextBlock x:Name="tbl" Loaded="MyNameTb_Loaded" Text="{StaticResource MyName}" /> </StackPanel> </code>
    <Window.Resources>
        <system:String x:Key="MyName">Will</system:String>
    </Window.Resources>
    <StackPanel>
        <StackPanel.Resources>
            <system:String x:Key="MyName">Will-StackPanel</system:String>
        </StackPanel.Resources>
        <TextBlock x:Name="tbl" Loaded="MyNameTb_Loaded" Text="{StaticResource MyName}" />
    </StackPanel>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>object myName = tbl.FindResource("MyName"); // return "Will-StackPanel"
</code>
<code>object myName = tbl.FindResource("MyName"); // return "Will-StackPanel" </code>
object myName = tbl.FindResource("MyName"); // return "Will-StackPanel"

but ReadLocalValue just returned the string value “Will” which is not my target

This is what I didn’t understand at all in your question.
The resource with the key “MyName ” is the string “Will”.
But this doesn’t suit you.
And what result did you want to get? Please explain in more detail.

Perhaps you are interested in checking if a key is in a dictionary?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>bool isContains = Resources.Contains("items");
</code>
<code>bool isContains = Resources.Contains("items"); </code>
bool isContains = Resources.Contains("items");

Or you can get all the keys from the dictionary:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>System.Collections.ICollection keys = Resources.Keys;
</code>
<code>System.Collections.ICollection keys = Resources.Keys; </code>
System.Collections.ICollection keys = Resources.Keys;

To search for a key hierarchically, you can create an extension method and use it:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> public static partial class FrameworkElementHelper
{
public static bool FindResourceKey(this FrameworkElement? element, object key)
{
while (element is not null)
{
if (element.Resources.Contains(key))
{
return true;
}
element = element.Parent as FrameworkElement;
}
return false;
}
}
</code>
<code> public static partial class FrameworkElementHelper { public static bool FindResourceKey(this FrameworkElement? element, object key) { while (element is not null) { if (element.Resources.Contains(key)) { return true; } element = element.Parent as FrameworkElement; } return false; } } </code>
    public static partial class FrameworkElementHelper
    {
        public static bool FindResourceKey(this FrameworkElement? element, object key)
        {
            while (element is not null)
            {
                if (element.Resources.Contains(key))
                {
                    return true;
                }
                element = element.Parent as FrameworkElement;
            }
            return false;
        }
    }
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>bool isFindKey = tbl.FindResourceKey("MyName"); // Return true
</code>
<code>bool isFindKey = tbl.FindResourceKey("MyName"); // Return true </code>
bool isFindKey = tbl.FindResourceKey("MyName"); // Return true

Perhaps you need to get the value of a key from the element property to which it is set using StaticResource?
If so, then it is not possible to do so.
DinamicResource creates an Expression and assigns it to a DependencyProperty. That is why DinamicResource can ONLY be used with DependencyProperty.
Unlike it, StaticResource returns the desired value, so it can be used with ANY property. And as a result, it is impossible to find out from the received value by which key it was obtained.

Specifically for your task, you can replace StaticResource with DynamicResource. But in general, this will not always be possible.

I would also advise you to change the approach to implementing the task. If you want to check the settings of its child elements from the control logic, it is better to use CustomControl with mandatory template elements.

12

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật