我如何为wpf中组合框的选定项目编写项目模板
我已经为 wpf 组合框创建了一个项目模板,以显示组合框项目中的多个属性和对象。
<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>
到目前为止,它很完美,它在组合框项目中显示了类型的 ID 和名称,但现在我希望当我选择一个项目时,它应该只在组合框的文本区域中显示所选项目的名称,而不是同时显示 ID 和名称。
有人可以帮忙吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用触发器来隐藏 ComboBox 显示区域中的 ID:
You can use a trigger to hide the ID in the ComboBox display area: