views:

51

answers:

1

Hello, I have the following scenario:

1 class called 'Widget' with the properties: ID, Code, Description
1 class called 'MyWidget' with a property: m_Widget As Widget
1 ComboBox

The ComboBox has a List(Of Widget) set as the ItemSource.

I create an instance of 'MyWidget' named MyWidget1 and I set the property values of the m_Widget to match one of the items in the 'ComboBox List(Of Widget)'. I then set the DataContext of the ComboBox to MyWidget1.Widget.

When I change the ComboBox selected item, only the ID property of 'MyWidget1.Widget' gets updated...

How do I get the object 'Widget' on 'MyWidget1' to be updated instead of just 'MyWidget1.Widget.ID'?

Here is a link to a sample project demonstrating this scenario:
http://www.webpersona.com/ObjectBinding.zip

Thanks in advance for any help :)


Update:

In the file: mwWindow.xaml

I replaced:

<ComboBox
Name="Widgets_ComboBox"
Height="21"
Margin="5,5,5,0"
DisplayMemberPath="Description"
SelectedItem="Widget"
SelectedValue="{Binding Path=ID}"
SelectedValuePath="ID"/>

With:

<ComboBox
Name="Widgets_ComboBox"
Height="21"
Margin="5,5,5,0"
DisplayMemberPath="Description"
SelectedItem="Widget"
SelectedValue="{Binding Widget}"/>

In the file: Application.xaml.vb

I replaced:

Public Sub ChangeSelectedMyWidget(ByVal sender As Object)
    Dim tmpWindow As mwWindow = CType(My.Application.MainWindow, mwWindow)
    My.Application.SelectedWidget = sender
    tmpWindow.Widgets_ComboBox.DataContext = My.Application.SelectedWidget.Widget
End Sub

With:

Public Sub ChangeSelectedMyWidget(ByVal sender As Object)
    Dim tmpWindow As mwWindow = CType(My.Application.MainWindow, mwWindow)
    My.Application.SelectedWidget = sender
    tmpWindow.Widgets_ComboBox.DataContext = My.Application.SelectedWidget
End Sub

And still no luck.

Now after selecting a rectangle it no longer selects the correctly matched item in the ComboBox.

In the unedited sample, after selecting a rectangle the binding selects the correctly matched item in the ComboBox and then when I change the ComboBox selection only the ID of the selected MyWidget.Widget gets updated.

I need exactly this behavior, except that all the properties of MyWidget.Widget get updated.

I suspected that I needed to bind on the Widget object rather than a property of the Widget earlier, but I couldn't get it to work, so I think that you are right in your answer, but it seems like I'm still missing something.

Also, thank you for your help, Ray. ;)

+2  A: 

Here's your problem:

SelectedValue="{Binding Path=ID}"
SelectedValuePath="ID"

This means that the ComboBox should update the ID value, not the Widget itself.

Replace it with this:

SelectedValue="{Binding Widget}"

and set the DataContext to MyWidget1. (If you set the DataContext to MyWidget1.Widget, it only has a reference to the Widget and has no possible way to update the link in MyWidget1.)

Ray Burns