Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 使用标记扩展绑定时出错:分析标记扩展时遇到未知属性_Wpf_Xaml_Data Binding_Radio Button - Fatal编程技术网

Wpf 使用标记扩展绑定时出错:分析标记扩展时遇到未知属性

Wpf 使用标记扩展绑定时出错:分析标记扩展时遇到未知属性,wpf,xaml,data-binding,radio-button,Wpf,Xaml,Data Binding,Radio Button,原则上,我开发了一种将单选按钮绑定到几乎任何东西的简洁方法: /// <summary>Converts an value to 'true' if it matches the 'To' property.</summary> /// <example> /// <RadioButton IsChecked="{Binding VersionString, Converter={local:TrueWhenEqual To='1.0'}}"/>

原则上,我开发了一种将单选按钮绑定到几乎任何东西的简洁方法:

/// <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();
    }
}
但这给了我一个错误:

类型的未知属性“到” 'MS.Internal.Markup.MarkupExtensionParser+UnknownMarkupExtension' 分析标记扩展时遇到错误


如果删除引号,错误仍然会发生。我做错了什么?

嵌套标记扩展可能会出现错误。尝试将自定义标记放入单独的DLL/项目或使用属性元素语法


    • WPF不能很好地处理嵌套标记扩展。要克服这一点,可以将标记扩展用作元素。这有点笨拙,也很难阅读,但它确实有效:

      <RadioButton GroupName="F1" Content="Filter Number One">
          <RadioButton.IsChecked>
              <Binding Path="Filter">
                  <Binding.Converter>
                      <local:TrueWhenEqual To={x:Static local:ViewModelClass.Filter1} />
                  </Binding.Converter>
              </Binding>
          </RadioButton.IsChecked>
      </RadioButton>
      
      
      
      另一种方法是声明转换器并将其用作静态资源:

      <Window.Resources>
          <local:TrueWhenEqual To={x:Static local:ViewModelClass.Filter1} x:Key="myConverter" />
      </Window.Resources>
      
      <RadioButton GroupName="F1" Content="Filter Number One"
                   IsChecked="{Binding Filter, Converter={StaticResource myConverter}}" />
      
      
      
      我在安装了.NET 4.6的机器上遇到了相同的错误。当我更新到.NET 4.7(开发者包)时,这个错误就消失了,没有任何代码更改。

      Clunky,但它可以工作。实际上它不工作,但它可以编译(我怀疑下一个问题是在我的MVVM框架UpdateControl中)。经确认,这个解决方案在没有我的MVVM框架的情况下可以工作。不过,在MarkupExtension上定义构造函数是一个更好的解决方案。然后我将其重命名为
      TrueWhenEqualTo
      ,这样读起来更自然:
      IsChecked=“{Binding Filter,Converter={local:TrueWhenEqualTo{x:Static local:ViewModelClass.Filter1}}}”
      。当然,“自然”是一个相对的术语;XAML在外行看来仍然像是胡言乱语;^)第二个链接有正确的答案(我指的是简单的答案)。我只需要定义一个构造函数
      public-TrueWhenEqual(object to){to=to;}
      ,然后使用
      converter={local:TrueWhenEqual{x:Static-local:ViewModelClass.Filter1}}调用转换器
      <RadioButton GroupName="F1" Content="Filter Number One">
          <RadioButton.IsChecked>
              <Binding Path="Filter">
                  <Binding.Converter>
                      <local:TrueWhenEqual To={x:Static local:ViewModelClass.Filter1} />
                  </Binding.Converter>
              </Binding>
          </RadioButton.IsChecked>
      </RadioButton>
      
      <Window.Resources>
          <local:TrueWhenEqual To={x:Static local:ViewModelClass.Filter1} x:Key="myConverter" />
      </Window.Resources>
      
      <RadioButton GroupName="F1" Content="Filter Number One"
                   IsChecked="{Binding Filter, Converter={StaticResource myConverter}}" />