ComboBox.ItemTemplate 未正确显示选择 [英] ComboBox.ItemTemplate not displaying selection properly

查看:29
本文介绍了ComboBox.ItemTemplate 未正确显示选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在问题 c# wpf - 不能同时设置 DisplayMemberPath 和ItemTemplate,我读到您可以替换 DisplayMemberPath 以在组合框中组合多个数据项.

In the question c# wpf - cannot set both DisplayMemberPath and ItemTemplate, I have read that you can replace the DisplayMemberPath to combine multiple data items in a ComboBox.

我使用 ItemTemplate 设置了我的组合框并成功填充了它.但是当我在 ComboBox 中选择一个项目时,我会显示 Data.MaterialNumber 而不是我选择的实际文本.

I set up my Combobox using ItemTemplate and it is successfully populated. But when I select an item in my ComboBox, I get Data.MaterialNumber displayed instead of the actual text I selected.

MaterialNumbers 是一个 ObservableCollection<>

MaterialNumbers is an ObservableCollection<>

有人能告诉我为什么我的 ComboBox 在选中项目后不能正确显示吗?

Can someone tell me why my ComboBox is not displaying the item correctly after it is selected?

// Binding from my ViewModel, which retrieves material numbers correctly.
ObservableCollection<MaterialNumber> MaterialNumbers = GetMaterialNumbers();

<ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding MaterialNumbers}"
    SelectedItem="{Binding MaterialNumber}"
    Validation.Error="View_Validator" Validation.ErrorTemplate="{StaticResource ErrorTemplateBorder}"
    IsEnabled="{Binding IsEnabled}" IsEditable="True" IsReadOnly="True" Margin="0,10,0,0">
    <ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <TextBlock.Text>
            <MultiBinding StringFormat="{}{0}: {1}">
                <Binding Path="Program.Name" />
                <Binding Path="MaterialNumberString" />
                <Binding UpdateSourceTrigger="PropertyChanged" />
                <Binding NotifyOnValidationError="True" />
                <Binding ValidatesOnDataErrors="True" />
            </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

推荐答案

感谢这篇文章 http://www.shujaat.net/2010/08/wpf-editable-combobox-with-datatemplate.html,我能够弄清楚为什么我的 ComboBox 没有显示正确选择项目时.这是因为我的 ComboBox 设置为 IsEditable="True".显然,当使用 ComboBox.ItemTemplate 并设置了 MultiBinding 时,ComboBox 无法确定要显示哪个项目,因此它会显示类.

Thanks to this article http://www.shujaat.net/2010/08/wpf-editable-combobox-with-datatemplate.html, I was able to figure out why my ComboBox did not display correctly when an item was selected. It is because my ComboBox was set to IsEditable="True". Apparently, when using a ComboBox.ItemTemplate and having MultiBinding set, the ComboBox cannot determine which item to display so it displays the class instead.

这篇关于ComboBox.ItemTemplate 未正确显示选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆