WPF 组合框上的选定项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14603355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:18:31  来源:igfitidea点击:

Selected item on WPF combobox

wpfxamlcomboboxselecteditemselectedvalue

提问by Witcher

I have some wpf combobox (xaml):

我有一些 wpf 组合框(xaml):

<ComboBox ItemsSource="{Binding Path=NonPositionedConcentrators}"
          SelectedValue="{Binding Path=SelectedNonPositionedConcentrator}"
          DisplayMemberPath="SerialNumber" />

SelectedNonPositionedConcentrator- is a Concentrator type. Something like:

SelectedNonPositionedConcentrator- 是集中器类型。就像是:

class Concentrator
{
...
public string SerialNumber {...}
...
public override ToString{ return "Some needed text..."; }
}

NonPositionedConcentrators- the list of Concentrator objects.

NonPositionedConcentrators- 集中器对象列表。

So, in application I see the combobox with serial numbers of NonPositionedConcentrators (because of DisplayMemberPath="SerialNumber"), but when I select something, then selected item shonw as "Some needed text...", see the image:

因此,在应用程序中,我看到带有 NonPositionedConcentrators 序列号的组合框(因为DisplayMemberPath="SerialNumber"),但是当我选择某些内容时,所选项目显示为"Some needed text...",请参见图像:

enter image description here

在此处输入图片说明

I have tried SelectedValuePath="SerialNumber", but it doesn't work... And I can't remove this - public override ToString{ return "Some needed text..."; }, becase I need...

我试过 SelectedValuePath="SerialNumber",但它不起作用......而且我无法删除它 - public override ToString{ return "Some needed text..."; },因为我需要......

回答by John Bowen

It looks like you have a custom ComboBox template which may be causing your problem. If it is using a TextBlock or ContentPresenter for the display of the selected item which is just binding the SelectedItem property and not pulling in any templates or other settings you would just get the ToString value no matter what. A properly constructed template will have something like what the default template uses at this spot:

看起来您有一个自定义 ComboBox 模板,这可能会导致您的问题。如果它使用 TextBlock 或 ContentPresenter 来显示所选项目,该项目只是绑定 SelectedItem 属性而不是拉入任何模板或其他设置,无论如何您都将获得 ToString 值。正确构造的模板将具有类似于此处默认模板使用的内容:

<ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
    Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}"
    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

回答by Andrey Gordeev

Try this:

尝试这个:

<ComboBox ItemsSource="{Binding Path=NonPositionedConcentrators}"
      SelectedItem="{Binding Path=SelectedNonPositionedConcentrator}"
      DisplayMemberPath="SerialNumber" />

EDIT:I've reproduced your example. My Concentrator class:

编辑:我已经复制了你的例子。我的集中器类:

public class Concentrator
{
    public string SerialNumber
    {
        get
        {
            return "123";
        }
    }
    public override string ToString()
    {
        return "Some needed text...";
    }
}

My ViewModel:

我的视图模型:

public class TestViewModel : ViewModelBase
{
    public ObservableCollection<Concentrator> NonPositionedConcentrators { get; set; }
    public Concentrator SelectedNonPositionedConcentrator { get; set; }
    public TestViewModel()
    {
        NonPositionedConcentrators = new ObservableCollection<Concentrator>();
        NonPositionedConcentrators.Add(new Concentrator());
        NonPositionedConcentrators.Add(new Concentrator());
        NonPositionedConcentrators.Add(new Concentrator());
    }
}

The combobox:

组合框:

    <ComboBox Height="23" Margin="25,12,103,0" Name="comboBox1" VerticalAlignment="Top" 
              ItemsSource="{Binding Path=NonPositionedConcentrators}"
              SelectedItem="{Binding Path=SelectedNonPositionedConcentrator}"
              SelectedValuePath="SerialNumber"
              DisplayMemberPath="SerialNumber" />

It works just fine for me!

它对我来说很好用!

回答by JMan

You could include an item template:

您可以包含一个项目模板:

       <ComboBox.ItemTemplate>
            <DataTemplate>
                //your stuff
            </DataTemplate>
        </ComboBox.ItemTemplate>