Nate Zaugg
Nate Zaugg

Reputation: 4218

Binding a Path in the class to an int property

I would like to bind a property in the source data to an int. Take for instance:

<ComboBox Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" ItemsSource="{Binding Makes}" SelectedItem="{Binding Path=Make_ID}" DisplayMemberPath="MakeDesc" />

From the ViewModel: public short Make_ID { get { return Vehicle.Make_ID; } set { Vehicle.Make_ID = value; OnPropertyChanged("Make_ID"); } }

Makes is a class that has ID, MakeDesc, etc. My View Model is interested in the selected make but only it's ID. I know I could do this with IValueConverters but I'd rather not have to do that -- I think there is a way to do that on the binding, I just can't remember how.

Upvotes: 0

Views: 68

Answers (1)

Nate Zaugg
Nate Zaugg

Reputation: 4218

The answer is to use SelectedValue and SelectedPath instead.

<ComboBox Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" ItemsSource="{Binding Makes}" SelectedValue="{Binding Path=Make_ID}" SelectedValuePath="ID" DisplayMemberPath="MakeDesc" />

Upvotes: 1

Related Questions