从 UserControl 中的资源访问代码隐藏中的变量
问题描述:
我有以下问题:我想访问 XAML 资源中定义的这三个变量之一
I have the following problem: I want to access one of these three vars, defined in XAML Resource
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../Themes/MainStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
<System:Int32 x:Key="maxVal">500</System:Int32>
<System:Int32 x:Key="minVal">250</System:Int32>
<System:Int32 x:Key="actualWidth">250</System:Int32>
</ResourceDictionary>
</UserControl.Resources>
我想像这样从代码隐藏文件中访问actualWidth"的值:
I want to access the value of "actualWidth" from Codebehind file like this:
private void MinMaxGraphicsMessageSink(bool minmax)
{
actualWidth = minmax ? 900 : 300;
}
但这行不通.有人可以帮忙吗?
But this doesn't work. Can someone help?
答
int maxVal = (Convert.ToInt32(FindResource("maxVal")));
同样是最小值
int minVal = (Convert.ToInt32(FindResource("minVal")));
设置资源的值,你可以这样做
to set value of a resource you can do like this
var resourceDictionary = this.Resources;
resourceDictionary["actualWidth"] = somevalue;