我想在另一个Double资源中引用另一个Double资源,类似于这样的写法:
100
{StaticResource width}
请问我该如何操作?
我怀疑这是不可能的,因为你引用了一个原子数据类型,它只能包含数值,既不是字段也不是属性。要允许这样做,你可能需要创建自己的数据类型。
编辑:通常情况下,你应该可以使用DynamicResource
来实现这个功能:
<DynamicResource x:Key="height" ResourceKey="width"/>
(Visual Studio 不喜欢这个,但它应该可以编译和工作)
嗯,我不确定你提供的示例是否可行,因为我无法将其绑定到“sys:Double”。
但除此之外,回答你的问题: 你可以使用转换器,而且非常简单。 添加这个类:
class DoubleConvertor : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后在 XAML 中引用此类(确保首先使用您项目的 xmlns):
<local:DoubleConvertor x:Key="DoubleConvertor" />
现在在你的绑定中,你可以这样做:
<UserControl Height="{Binding path={StaticResource width}, Converter={StaticResource DoubleConvertor} />