简体   繁体   English

带有自定义项目模板的 ComboBox 在选择值时显示模板而不是文本

[英]ComboBox with custom item template showing template and not text when selecting value

I have created a ComboBox with a custom items template like this:我创建了一个带有自定义项目模板的ComboBox ,如下所示:

<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. ComboBox绑定到视图模型中的Clase值。 Clase is a variable of type CLASE_TYPE which is a class that looks like this: Clase是一个CLASE_TYPE类型的变量,它是一个如下所示的类:

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

The result looks like this:结果如下所示:

组合框下拉项

The problem comes when I select a value, the selected value is showing in the ComboBox like this:当我选择一个值时,问题出现了,选择的值显示在ComboBox如下所示:

选定的组合框项目

How can I avoid this and only show the value of CLASE of the CLASE_TYPE class?如何避免这种情况而只显示CLASE_TYPE类的CLASE的值?

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.您可以为按钮命名,并通过将其Visibility设置为Collapsed来向数据模板添加触发器以隐藏按钮,无论何时选择它。 The trigger needs to check if there is a parent ComboBoxItem or not ( x:Null ), checking IsSelected does not work.触发器需要检查是否有父ComboBoxItem ( x:Null ),检查IsSelected不起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM