2
votes

i have created an item template for wpf combo box to display multiple properties of and object in a combo box item.

<ComboBox x:Name="cmbType" Grid.Column="2" Grid.Row="0" Grid.ColumnSpan="1"
        SelectedItem="{Binding Current.Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEditable="False"
        ItemsSource="{Binding Types, Mode=OneWay}" DropDownClosed="cmbIncidentType_DropDownClosed" TabIndex="601">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="4" />
                    <ColumnDefinition Width="*" />

                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding ID}" />
                <TextBlock Grid.Column="2" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

till now its perfect it shows both id and name of a type in combo box item but now i want when i select an item it should only display name of selected item in the text area of combo box instead of displaying both ID and name.

Can anyone please help?

1
Read the editing help before breaking the formatting again.H.B.

1 Answers

1
votes

You can use a trigger to hide the ID in the ComboBox display area:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding ID}" Margin="0,0,4,0">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}, Path=IsSelected}" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <TextBlock Text="{Binding Name}"/>
    </StackPanel>
</DataTemplate>