我有一个无窗口应用程序,它只包含一个由 ResourceDictionary 填充的 App.xaml。如何从其代码隐藏访问该词典中的控件?
最佳答案
尝试了各种方法都不行,比如通过VisualTreeHelper获取控件,直接通过名称访问控件,解决方法出奇的简单:
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>
ResourceDictionary.xaml.cs:
// Example with a button control
Button myButton= this["myButtonName"] as Button;
if(myButton != null)
{
// Do something
}
关于c# - 如何从其隐藏代码访问资源字典中的控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26420676/