【发布时间】:2021-02-11 09:43:11
【问题描述】:
我创建了一个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>
ComboBox 绑定到视图模型中的Clase 值。 Clase 是 CLASE_TYPE 类型的变量,它是一个如下所示的类:
public partial class CLASE_TYPE
{
public decimal ID { get; set; }
public string CLASE { get; set; }
}
结果如下:
当我选择一个值时,问题就出现了,所选值显示在ComboBox 中,如下所示:
如何避免这种情况,只显示CLASE_TYPE 类的CLASE 的值?
【问题讨论】: