使用标记扩展绑定时出错:解析标记扩展时遇到未知属性 [英] Error when binding using markup extensions: Unknown property encountered while parsing a Markup Extension
问题描述
原则上,我开发了一种巧妙的方法将 RadioButtons 绑定到几乎任何东西:
In principle, I developed a neat way to bind RadioButtons to almost anything:
/// <summary>Converts an value to 'true' if it matches the 'To' property.</summary>
/// <example>
/// <RadioButton IsChecked="{Binding VersionString, Converter={local:TrueWhenEqual To='1.0'}}"/>
/// </example>
public class TrueWhenEqual : MarkupExtension, IValueConverter
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object To { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return object.Equals(value, To);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value) return To;
throw new NotSupportedException();
}
}
例如,您可以使用它来将 RadioButtons 绑定到字符串属性,如下所示(这是一个众所周知的错误,您必须为每个 RadioButton 使用唯一的 GroupName):
For example, you can use this to bind RadioButtons to a string property as follows (it is a well-known bug that you must use a unique GroupName for each RadioButton):
<RadioButton GroupName="G1" Content="Cat"
IsChecked="{Binding Animal, Converter={local:TrueWhenEqual To='CAT'}}"/>
<RadioButton GroupName="G2" Content="Dog"
IsChecked="{Binding Animal, Converter={local:TrueWhenEqual To='DOG'}}"/>
<RadioButton GroupName="G3" Content="Horse"
IsChecked="{Binding Animal, Converter={local:TrueWhenEqual To='HORSE'}}"/>
现在,我想使用名为 Filter1
和 Filter2
的 public static readonly
对象作为我的 RadioButtons 的值.所以我尝试了:
Now, I would like to use public static readonly
objects called Filter1
and Filter2
as the values of my RadioButtons. So I tried:
<RadioButton GroupName="F1" Content="Filter Number One"
IsChecked="{Binding Filter, Converter={local:TrueWhenEqual To='{x:Static local:ViewModelClass.Filter1}'}}"/>
<RadioButton GroupName="F2" Content="Filter Number Two"
IsChecked="{Binding Filter, Converter={local:TrueWhenEqual To='{x:Static local:ViewModelClass.Filter2}'}}"/>
但这给了我一个错误:
类型的未知属性To"'MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension'解析标记扩展时遇到.
Unknown property 'To' for type 'MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension' encountered while parsing a Markup Extension.
如果我删除引号,错误仍然会发生.我做错了什么?
The error still occurs if I remove the quotes. What am I doing wrong?
推荐答案
这是嵌套 MarkupExtensions 可能发生的错误.尝试将您的自定义标记放入单独的 DLL/项目或使用属性元素语法.
It's a bug that can occur with nested MarkupExtensions. Try putting your custom Markup into a separate DLL/Project or use property element syntax.
这篇关于使用标记扩展绑定时出错:解析标记扩展时遇到未知属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!