【问题标题】:How to bind a property inside a child element?如何在子元素中绑定属性?
【发布时间】:2020-12-10 10:13:44
【问题描述】:

我想使用两个属性值来定义一个元素是否启用。 到目前为止,以下行决定启用/禁用一个元素。 IsEnabled 是根据 绑定到someContext.SomeObject.Count

<ToggleButton Style="{StaticResource CheckButtonStyle}" Margin="1"
              IsChecked="{Binding Path=someContext[3], ElementName=MyElementName, IsAsync=True}"
              IsEnabled="{Binding Path=someContext.SomeObject.Count, ElementName=MyElementName, Converter={StaticResource MyConverter}, ConverterParameter=3}" />

我将转换器更改为 IMultiValueConverter 并且 我将 XAML 更改为以下内容:

<ToggleButton Style="{StaticResource CheckButtonStyle}" Margin="1"
              IsChecked="{Binding Path=someContext[3], 
              ElementName=MyElementName, IsAsync=True}">
   <ToggleButton.IsEnabled>
      <MultiBinding Converter="{StaticResource MyMultiValueConverter}" ConverterParameter="3">
         <Binding Path="{someContext.SomeObject.Count}"/>
         <Binding Path="{IsConditionFullfilled}/>
      </MultiBinding>
   </ToggleButton.IsEnabled>
</ToggleButton>

但是在第一个版本中,大括号内的绑定被识别, 但在第二个版本中,我得到了:

“未找到类型'someContext'。验证您没有丢失 一个程序集引用,并且所有引用的程序集都已构建。”

我认为&lt;ToggleButton.IsEnabled&gt;&lt;/ToggleButton.IsEnabled&gt; 无法访问与其父级&lt;ToggleButton /&gt; 相同的命名空间

我该如何解决这个问题?

【问题讨论】:

  • 去掉名称周围的花括号,例如:&lt;Binding Path="someContext.SomeObject.Count"/&gt;

标签: c# wpf xaml data-binding


【解决方案1】:

删除名称周围的花括号,否则它们将被解释为markup extensions

当用于提供属性值时,将标记扩展序列与 XAML 处理器区分开来的语法是左大括号和右大括号({ 和 })的存在。然后,标记扩展的类型由紧跟在左大括号后面的字符串标记标识。

此外,如果仍然需要,请添加原始绑定中的 ElementName

<MultiBinding Converter="{StaticResource MyMultiValueConverter}" ConverterParameter="3">
   <Binding Path="someContext.SomeObject.Count" ElementName="MyElementName"/>
   <Binding Path="IsConditionFullfilled"/>
</MultiBinding>

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2011-02-23
    • 1970-01-01
    • 2023-04-04
    • 2012-05-28
    • 1970-01-01
    相关资源
    最近更新 更多