Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

.net - Displaying the selected item differently in ComboBox

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>

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

EDIT

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.)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...