【问题标题】:Binding style property outside itemsource?在itemssource之外绑定样式属性?
【发布时间】:2017-11-18 14:46:41
【问题描述】:

我有一个这样的组合框:

   <ComboBox x:Name="CountryMenuComboBox" 
        ItemsSource="{Binding Countries}">

        <ComboBox.ItemContainerStyle>
            <Style>
                <Setter Property="IsEnabled" Value="{Binding IsRemoving}" />
            </Style>
        </ComboBox.ItemContainerStyle>

我需要做的是使用属性IsRemoving 启用或禁用组合框中的项目,但此属性不在itemsource Countries 内,因此我需要在itemsource 之外访问。我怎样才能为样式做到这一点?

【问题讨论】:

  • 如果您不能告诉我们房产在哪里,则无法回答问题。它在拥有Countries 属性的父视图模型上吗?如果是这样,请尝试IsEnabled=“{Binding DataContext.IsRemoving, RelativeSource={RelativeSource AncestorType=ComboBox}}”
  • @EdPlunkett 是的,抱歉,国家属性位于同一个视图模型中

标签: c# wpf xaml


【解决方案1】:

IsRemoving 是拥有Countries 属性的父视图模型的属性吗?如果是这样,请尝试&lt;Setter Property=“IsEnabled” Value=“{Binding DataContext.IsRemoving, RelativeSource={RelativeSource AncestorType=ComboBox}}” /&gt;

【讨论】:

  • 我不明白一件事:IsEnabled 应该放在 setter 上吗?还是在哪里?
  • 现在在同一个地方。只需添加 RelativeSource 部分。
  • 类似:&lt;Setter Property="IsEnabled={Binding }" ..?
  • 就像你有它一样,但像我说的那样改变绑定。
  • 哦,你的意思是 IsRemoving 不是 IsEnabled,错字错误?
猜你喜欢
  • 2018-08-27
  • 2012-01-08
  • 2016-03-28
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多