从 UserControl 中的资源访问代码隐藏中的变量 [英] Access variable in code-behind from Resources in UserControl
本文介绍了从 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;
这篇关于从 UserControl 中的资源访问代码隐藏中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文