I'm not sure if this is possible or not. I have created a ComboBox like this...
<ComboBox Name="testType"
Margin="0,5,0,0"
IsTextSearchEnabled="True"
DisplayMemberPath="Description"
SelectedValue="{Binding MyClass.Id, Mode=TwoWay}"
SelectedValuePath="Id"/>
with code behind that loads up the available options...
DataTable testCatList = TestTypeBase.GetAll();
testType.ItemsSource = testCatList.DefaultView;
so that it properly displays all the items in MyClass. When the user makes a selection, the Id field in MyClass gets updated as intended. Great.
Here's my problem: My testCatList contains both the Id and the Description for each of the rows, and I'd like both of those fields to be bound to the current MyClass instance. So here's what I tried:
<ComboBox Name="testType"
Margin="0,5,0,0"
IsTextSearchEnabled="True"
DisplayMemberPath="Description">
<ComboBox.SelectedValue>
<MultiBinding Converter="{???}">
<Binding Path="MyClass.Id" Mode="TwoWay"/>
<Binding Path="MyClass.Description" Mode="TwoWay"/>
</MultiBinding>
</ComboBox.SelectedValue>
</ComboBox>
Here, I'd like to have MyClass.Id set to the selected Id and the MyClass.Description set to the selected Description. As you can see, I have removed the SelectedValuePath because I don't just want the Id any more. But I don't know what to use for the Converter (see the question marks above).
Any ideas, oh experts of StackOverflow? Thanks.
If I understood correctly try using SelectedItem
property instead of SelectedValue
.
In that way you will get the selected MyClass
instance and of course all of its properties.
SelectedItem="{Binding CurrentSelectedMyClassItem}"