使用标记扩展进行绑定时出错:解析标记扩展时遇到的未知属性 [英] Error when binding using markup extensions: Unknown property encountered while parsing a Markup Extension
问题描述
///< summary>转换如果它与To属性匹配,则值为true。< / 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;组;
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;
抛出新的NotSupportedException();
}
}
例如,您可以使用它将RadioButtons绑定到一个字符串属性如下(它是一个众所周知的错误,您必须为每个RadioButton使用唯一的GroupName):
; RadioButton GroupName =G1Content =Cat
IsChecked ={Binding Animal,Converter = {local:TrueWhenEqual To ='CAT'}}/>
< RadioButton GroupName =G2Content =Dog
IsChecked ={Binding Animal,Converter = {local:TrueWhenEqual To ='DOG'}}/>
< RadioButton GroupName =G3Content =Horse
IsChecked ={Binding Animal,Converter = {local:TrueWhenEqual To ='HORSE'}}/>
现在,我想使用 public static readonly
对象称为 Filter1
和 Filter2
作为我的RadioButtons的值。所以我试过:
< RadioButton GroupName =F1Content =Filter Number One
IsChecked = {Binding Filter,Converter = {local:TrueWhenEqual To ='{x:Static local:ViewModelClass.Filter1}'}}/>
< RadioButton GroupName =F2Content =Filter Number Two
IsChecked ={Binding Filter,Converter = {local:TrueWhenEqual To ='{x:Static local:ViewModelClass.Filter2} }}/>
但这给我一个错误:
未知属性'to'用于类型
'MS.Internal.Markup.MarkupExtensionParser + UnknownMarkupExtension'
在解析标记扩展时遇到。
如果我删除引号,仍然会出现错误。我做错了什么?
这是一个嵌套的MarkupExtensions可能发生的错误。尝试将自定义标记放入单独的DLL / Project或使用属性元素语法。
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();
}
}
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'}}"/>
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}'}}"/>
But this gives me an error:
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?
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屋!