0
<ComboBox 
    x:Name="comboBox" 
    Margin="281.4,160,259.995,159.958" 
    d:LayoutOverrides="Height" 
    ItemsSource="{Binding _US_STATES}"
    SelectedIndex="0" 
    SelectedValue="{Binding SelectedState}" 
    SelectedValuePath="{Binding Path=_US_STATES/SHORT}"
    >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Border Padding="2">
                <TextBlock Text="{Binding LONG}" />
            </Border>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

The above is my xaml version of a ComboBox that I am displaying. I can get it to display correctly because it shows the right data. When I try to get the value that it is selected, it is always RxTracking.model.US_STATES not the value it should be.

The US_STATES looks like this:

public class US_STATES : ObservableObject
{
    private string _long;
    private string _short;

    public string LONG
    {
        get { return _long; }
        set { Set("LONG", ref _long, value); }
    }

    public string SHORT
    {
        get { return _short; }
        set { Set("SHORT", ref _short, value); }
    }

    public static ObservableCollection<US_STATES> GetAllStates()
    {
        ObservableCollection<US_STATES> ALL = new ObservableCollection<US_STATES>
        {
            new US_STATES {LONG="ALABAMA",SHORT="AL" },
            new US_STATES {LONG="Alaska", SHORT = "AK"},
            new US_STATES {LONG = "Arizona", SHORT = "AZ"},
            etc ...

I am getting this is in the error window:

System.Windows.Data Error: 40 : BindingExpression path error: 'AL' property not found on 'object' ''US_STATES' (HashCode=8402670)'. BindingExpression:Path=AL; DataItem='US_STATES' (HashCode=8402670); target element is 'ComboBox' (Name='comboBox'); target property is 'NoTarget' (type 'Object')

System.Windows.Data Error: 40 : BindingExpression path error: 'AL' property not found on 'object' ''US_STATES' (HashCode=15232780)'. BindingExpression:Path=AL; DataItem='US_STATES' (HashCode=15232780); target element is 'ComboBox' (Name='comboBox'); target property is 'NoTarget' (type 'Object')
barakisbrown
  • 1,293
  • 3
  • 9
  • 15
  • Possible duplicate of [WPF IsEditable=true ComboBox filled with objects displays the ToString() as the selected item](https://stackoverflow.com/questions/1844156/wpf-iseditable-true-combobox-filled-with-objects-displays-the-tostring-as-the) – Huseyin Yagli Oct 26 '17 at 09:49

2 Answers2

1

The SelectedValuePath property is a string. It should be the name of the property of the list item class that you want to use for the selected value.

If you want to use the SHORT property of US_STATES, that's easy:

SelectedValuePath="SHORT"

That's why it's weeping and wailing about 'AL' property not found on 'object' ''US_STATES': The binding updated SelectedValuePath with the value of _US_STATES/SHORT (that's the "current item", for which it uses the first one since ObservableCollection doesn't have a CurrentItem property -- that gets into CollectionViewSource and ICollectionView, which you don't need to worry about unless you're sorting or filtering stuff in XAML), which was "AL". So the ComboBox dutifully tries to find the AL property of US_STATES, which doesn't exist.

By the way, you can spare yourself the trouble of writing a template and simply set DisplayMemberPath:

DisplayMemberPath="LONG"
0

You need to set the display value property on the combobox or override ToString in the States class.

Whats happening is that the combobox is calling ToString on the objects stored within it and if ToString isn't overridden then it returns the class name.

Dmitry K.
  • 972
  • 6
  • 16