简体   繁体   English

WPF组合框上的选定项目

[英]Selected item on WPF combobox

I have some wpf combobox (xaml): 我有一些WPF组合框(XAML):

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

SelectedNonPositionedConcentrator - is a Concentrator type. SelectedNonPositionedConcentrator是集中器类型。 Something like: 就像是:

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" ),但是当我选择某项时,然后选择项shonw作为"Some needed text..." ,请参见图像:

在此处输入图片说明

I have tried SelectedValuePath="SerialNumber", but it doesn't work... And I can't remove this - public override ToString{ return "Some needed text..."; } 我已经尝试了SelectedValuePath =“ SerialNumber”,但是它不起作用...而且我无法删除此- public override ToString{ return "Some needed text..."; } public override ToString{ return "Some needed text..."; } , becase I need... public override ToString{ return "Some needed text..."; } ,becase的我要...

It looks like you have a custom ComboBox template which may be causing your problem. 看来您有一个自定义的ComboBox模板,这可能会导致您的问题。 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. 如果它使用TextBlock或ContentPresenter来显示所选项目,而该项目只是绑定SelectedItem属性而没有拉入任何模板或其他设置,则无论如何都将只获得ToString值。 A properly constructed template will have something like what the default template uses at this spot: 正确构造的模板将具有类似于默认模板在此处使用的内容:

<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}"/>

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: 我的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! 它对我来说很好!

You could include an item template: 您可以包括一个项目模板:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM