如何从具有C#的对象获取资源密钥?

如何从具有C#的对象获取资源密钥?,c#,wpf,wpf-controls,resourcedictionary,C#,Wpf,Wpf Controls,Resourcedictionary,现在,我有一个.xaml文件,如下所示: <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Button Content="{DynamicResource resource1}" Height="50" Width="100" /> </

现在,我有一个.xaml文件,如下所示:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Button Content="{DynamicResource resource1}" Height="50" Width="100" />
</Grid>


现在我想获得这个资源键(“resource1”)以显示给用户。我可以得到这个元素:Button,我可以得到它的“Content”属性值:test1,但是现在,我只想知道它引用了哪些资源?除了解析字符串,还有其他方法吗

不确定你的计划是否正确?为什么要用XamlReader加载外部文件?为什么在网格中?你想要哪把钥匙?因为您的示例中没有x:KEY?这不起作用…BindingOperations.GetBinding(目标,ContentControl.ContentProperty);所以think不适用于内容==>顺便说一句,我不明白你为什么想要得到所有这些…你正在失去wpf的所有力量…好luckI在下面的链接中找到了答案
<Application
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:WpfApp1"
         xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="WpfApp1.App"
         StartupUri="MainWindow.xaml">
<Application.Resources>

    <System:String x:Key="resource1">test1</System:String>
    <System:String x:Key="resource2">test2</System:String>

</Application.Resources>