3

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

4

1 回答 1

3

尝试了各种方法都行不通,比如通过VisualTreeHelper获取控件,直接通过name访问控件,解决方法出奇的简单:

资源字典.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>

资源字典.xaml.cs:

// Example with a button control
Button myButton= this["myButtonName"] as Button;

if(myButton != null)
{
 // Do something
}
于 2014-10-17T08:12:07.397 回答