Question

I have some wpf combobox (xaml):

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

SelectedNonPositionedConcentrator - is a Concentrator type. Something like:

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

NonPositionedConcentrators - the list of Concentrator objects.

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:

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...

Was it helpful?

Solution

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:

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

OTHER TIPS

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!

You could include an item template:

       <ComboBox.ItemTemplate>
            <DataTemplate>
                //your stuff
            </DataTemplate>
        </ComboBox.ItemTemplate>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top