Skip to main content
 首页 » 编程设计

c#之如何使用样式和 DataTrigger 绑定(bind)到 XAML 中项目源的计数

2024年02月06日11softidea

如果我按名称调用列表,我可以绑定(bind)到项目源,但是,我无法通过获取每个单独组合框上的项目计数来使绑定(bind)正常工作。

这是我想在 XAML 中执行的操作。我需要更改什么才能使此绑定(bind)生效?

<Grid.Resources> 
  <Style TargetType="ComboBox">                              
     <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=Items.Count}" Value="0"> 
          <Setter Property="IsEnabled" Value="False"></Setter> 
       </DataTrigger> 
     </Style.Triggers> 
   </Style> 
</Grid.Resources> 
 
   <ComboBox 
    ItemsSource="{Binding MyList}"                
    SelectedItem="{Binding SelectedElement}" 
    ItemTemplate="{StaticResource MyTemplate}"> 
      </ComboBox> 

请您参考如下方法:

包括 RelativeSource绑定(bind)中的组件:

<DataTrigger Binding="{Binding Path=Items.Count, RelativeSource={RelativeSource Self}}"  
             Value="0" 
             > 

您当前拥有它的方式是,绑定(bind)子系统将在您设置为 ComboBox 的 DataContext 的任何内容上查找 Items.Count 属性。