0

I have created a ComboBox with a custom items template like this:

<ComboBox ItemsSource="{Binding Clases}" SelectedValue="{Binding Clase}" Style="{StaticResource ComboBoxStyle}" Grid.Column="0">
   <ComboBox.ItemContainerStyle>
      <Style TargetType="ComboBoxItem">
         <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
      </Style>
   </ComboBox.ItemContainerStyle>
   <ComboBox.ItemTemplate>
      <DataTemplate>
         <Grid Margin="0 2">
            <Grid.ColumnDefinitions>
               <ColumnDefinition/>
               <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0">
               <TextBlock Text="{Binding CLASE}" VerticalAlignment="Center"/>
            </Grid>
            <Button Command="{Binding DataContext.BorrarClaseCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" CommandParameter="{Binding}" ToolTip="BORRAR CLASE" Grid.Column="1">
               <materialDesign:PackIcon Kind="Delete"/>
            </Button>
         </Grid>
      </DataTemplate>
   </ComboBox.ItemTemplate>
</ComboBox>

The ComboBox is binding to the Clase value in the view model. Clase is a variable of type CLASE_TYPE which is a class that looks like this:

public partial class CLASE_TYPE
{
    public decimal ID { get; set; }
    public string CLASE { get; set; }
}

The result looks like this:

Combo box drop-down items

The problem comes when I select a value, the selected value is showing in the ComboBox like this:

Selected combo box item

How can I avoid this and only show the value of CLASE of the CLASE_TYPE class?

thatguy
  • 21,059
  • 6
  • 30
  • 40
Paul Miranda
  • 738
  • 17
  • 39
  • Does this answer your question? [Displaying the selected item differently in ComboBox](https://stackoverflow.com/questions/1466592/displaying-the-selected-item-differently-in-combobox) – Rekshino Oct 29 '20 at 08:48
  • See also [WPF: How to customize SelectionBoxItem in ComboBox](https://stackoverflow.com/questions/2214696/wpf-how-to-customize-selectionboxitem-in-combobox). – Rekshino Oct 29 '20 at 08:49

1 Answers1

1

You can give your button a name and add a trigger to the data template that hides the button by setting its Visibility to Collapsed, whenever it is selected. The trigger needs to check if there is a parent ComboBoxItem or not (x:Null), checking IsSelected does not work.

<DataTemplate>
   <Grid Margin="0 2">
      <Grid.ColumnDefinitions>
         <ColumnDefinition/>
         <ColumnDefinition Width="Auto"/>
      </Grid.ColumnDefinitions>
      <Grid Grid.Column="0">
         <TextBlock Text="{Binding CLASE}" VerticalAlignment="Center"/>
      </Grid>
      <Button x:Name="BorrarClaseButton" ToolTip="BORRAR CLASE" Grid.Column="1">
         <materialDesign:PackIcon Kind="Delete"/>
      </Button>
   </Grid>
   <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}}}" Value="{x:Null}">
         <Setter TargetName="BorrarClaseButton" Property="Visibility" Value="Collapsed"/>
      </DataTrigger>
   </DataTemplate.Triggers>
</DataTemplate>

Alternatively, you could create a style for the button that does the same.

<Button ToolTip="BORRAR CLASE" Grid.Column="1">
   <Button.Style>
      <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
         <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}}}" Value="{x:Null}">
               <Setter Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Button.Style>
   <materialDesign:PackIcon Kind="Delete"/>
</Button>
thatguy
  • 21,059
  • 6
  • 30
  • 40