根据ItemsControl的Item Index隐藏控件

时间:2015-01-23 12:59:16

标签: c# wpf xaml

在我的xaml中,我有一个ItemsControl。是否可以在ItemsControl上拥有ItemIndex属性?基本上我想隐藏一个子控件(TxtNodeData),如果Item索引是1

<ItemsControl ItemsSource="{Binding ConditionList}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <WrapPanel>
          <TextBlock Text="{Binding NodeData}" Name="TxtNodeData"/>
          <Button Content="+" />
          <ComboBox ItemsSource="{Binding NodeNames}" DisplayMemberPath="name" SelectedValue="{Binding ConditionalNodeId, Mode=TwoWay}" SelectedValuePath="id"> </ComboBox>
          <Button Content="-" />
       </WrapPanel>
     </DataTemplate>
   </ItemsControl.ItemTemplate>

1 个答案:

答案 0 :(得分:4)

您可以将AlternationCount组合设置为列表中的项目数并将AlternationIndex触发为1

<ItemsControl ItemsSource="{Binding ConditionList}" AlternationCount="{Binding ConditionList.Count}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <TextBlock Text="{Binding NodeData}" Name="TxtNodeData">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <Style.Triggers>
                                <DataTrigger 
                                    Binding="{Binding 
                                        RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}, 
                                        Path=(ItemsControl.AlternationIndex)}" 
                                    Value="1">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
                <!-- other controls -->
            </WrapPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>