Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从资源字典中的控件的代码隐藏处访问该控件?_C#_Wpf_Resourcedictionary_Windowless - Fatal编程技术网

C# 如何从资源字典中的控件的代码隐藏处访问该控件?

C# 如何从资源字典中的控件的代码隐藏处访问该控件?,c#,wpf,resourcedictionary,windowless,C#,Wpf,Resourcedictionary,Windowless,我有一个无窗口应用程序,它只包含一个App.xaml,由ResourceDictionary填充。如何从字典中的代码隐藏访问控件?在尝试了各种方法之后,这些方法都不起作用,例如通过VisualTreeHelper获取控件,通过name直接访问控件,解决方案非常简单: ResourceDictionary.xaml <ResourceDictionary x:Class="My.Namespace" xmlns="http://schemas.micr

我有一个无窗口应用程序,它只包含一个App.xaml,由ResourceDictionary填充。如何从字典中的代码隐藏访问控件?

在尝试了各种方法之后,这些方法都不起作用,例如通过VisualTreeHelper获取控件,通过name直接访问控件,解决方案非常简单:

ResourceDictionary.xaml

<ResourceDictionary x:Class="My.Namespace"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Button x:Key="myButtonName" />

</ResourceDictionary>

伟大的发现!happy编码:)在同一分钟内询问并回答?你一定是在发帖之前就把答案打印出来了。
// Example with a button control
Button myButton= this["myButtonName"] as Button;

if(myButton != null)
{
 // Do something
}