显示所选择的项目不同的方式在组合框组合、不同、方式、项目

2023-09-04 00:27:51 作者:永不凋零的花

我在,我成立了一个组合框的 ItemTemplate中看起来是这样的:

I have a combo box in which I set up an ItemTemplate that looks something like this:

<ComboBox.ItemTemplate>
  <DataTemplate>
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding Piece.NoPiece}" Width="50" />
      <TextBlock Text="{Binding Piece.Description}" Width="170" />
      <TextBlock Text="{Binding Piece.Categorie.NomCategorie}" />
    </StackPanel>
  </DataTemplate>
</ComboBox.ItemTemplate>

正如你所看到的,我就是为了让用户看到不同的资料片三列。不过,我想选择的项目在组合显示仅第二列。换句话说,有没有办法有一个 ItemTemplate中,在当你向下滚动与当它关闭,您只看到选择不同的方式显示项目?

As you can see, I got three columns that let the user see different piece of information. However, I would like the selected item in the combo to display only the second column. In other word, is there a way to have an ItemTemplate that displays items in a different manner when you scroll down versus when it's closed and you only see the selection?

推荐答案

您可以用触发器做到这一点:

You can do it with triggers:

<ComboBox.ItemTemplate>
  <DataTemplate>
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding Piece.NoPiece}" Width="50" x:Name="Column1" />
      <TextBlock Text="{Binding Piece.Description}" Width="170" />
      <TextBlock Text="{Binding Piece.Categorie.NomCategorie}" x:Name="Column3" />
    </StackPanel>
    <DataTemplate.Triggers>
      <!-- This trigger fires for the selected item in the drop-down list -->
      <DataTrigger Binding="{Binding 
                       RelativeSource={RelativeSource Mode=FindAncestor, 
                                                      AncestorType=ComboBoxItem},
                       Path=IsSelected}" 
        Value="True">
        <Setter TargetName="Column1" Property="Visibility" Value="Hidden" />
        <Setter TargetName="Column3" Property="Visibility" Value="Hidden" />
      </DataTrigger>

      <!-- This trigger fires for the selected item (ie the one that's
           visible when the popup is closed -->
      <DataTrigger Binding="{Binding 
                       RelativeSource={RelativeSource Mode=FindAncestor, 
                                                      AncestorType=ComboBoxItem}}"
                   Value="{x:Null}">
        <Setter TargetName="Column1" Property="Visibility" Value="Hidden" />
        <Setter TargetName="Column3" Property="Visibility" Value="Hidden" />
      </DataTrigger>
    </DataTemplate.Triggers>
  </DataTemplate>
</ComboBox.ItemTemplate>

修改

我已经更新了XAML来说明如何替代格式应用于选定项目时,弹出窗口崩溃(我不知道那是什么地方叫。)

I've updated the XAML to show how to apply the alternative formatting to the selected item when the popup is collapsed (I'm not sure what that area is called.)

诀窍是,在下拉区域的项目都包含在 ComboBoxItem 在逻辑树的对象。该的RelativeSource 绑定将查找类型作为祖先的对象。

The trick is that items in the drop-down area are contained within ComboBoxItem objects in the logical tree. The RelativeSource binding looks for an object of that type as an ancestor.

如果它发现它,它假设该项目是在树(并检查它是否处于选中状态) 如果它没有找到(),那么它假定的产品组合框区域,而不是弹出 If it finds it, it assumes that the item is in the tree (and checks whether it's selected) If it's not found (null) then it assumes the item is in the combo box area rather than the popup

这就会崩溃,如果你不知何故,有一个组合框的另一个组合框的项目模板中。我不认为我想,虽然使用的用户界面!

This would fall apart if you, somehow, had a combo box within the item template of another combo box. I don't think I'd like to use that UI though!