I have a ComboBox populated with Checkboxes.
The ItemsSource of the ComboxBox is bound to a List of objects which are to be bound the check-boxes; a ViewModel. The view-model is a simple object (of type MultiSelectDropDownItem) which has a boolean field names Selected.
Now, the ItemsSource is set programmatically. This is fine; the attributes of the check-boxes which are bound the view-models are all populated properly, and if I check/uncheck the checkboxes, the change is reflected in the view-model. So to me, the two-way binding is working.
The problem is when I updated the Selected property of one of these MultiSelectDropDownItems elsewhere. The property fires off a PropertyChanged event, but this time the change is NOT reflected in the Checkbox.
I've been looking at this for ages now and for the life of me I cannot figure out why the change is not being updated - why does the PropertyChanged event NOT update the CheckBox, even with the object behind the check-box has had its property changed?
XAML:
<ComboBox x:Name="FieldOptions"
ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Stretch"
Height="30"
KeyDown="FieldOptions_OnKeyDown">
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="checkbox"
Content="{Binding Path=Text}"
Uid="{Binding Path=ID}"
IsChecked="{Binding Path=Selected, Mode=TwoWay}"
FontStyle="Normal"
Foreground="Black"
Checked="CheckBox_OnChecked"
Unchecked="CheckBox_Unchecked"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Code-behind (excuse the VB- not my choice!):
Dim items As List(Of MultiSelectDropDownItem) = CreateDropdownItems()
FieldOptions.ItemsSource = items
''' <summary>
''' Represents an item for a Multi-Select drop-down control; a 'View-Model' for combo-items.
''' </summary>
''' <remarks>LN - 08/01/2013</remarks>
Private Class MultiSelectDropDownItem
Inherits clsTemplateControlText
Implements INotifyPropertyChanged
Private _selected As Boolean
Public Property Selected() As Boolean
Get
Return _selected
End Get
Set(value As Boolean)
If (value <> _selected) Then
_selected = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(value))
End If
End Set
End Property
Public Sub New(ByVal tct As clsTemplateControlText, ByVal selected As Boolean)
ID = tct.ID
ControlID = tct.ControlID
Text = tct.Text
ParentID = tct.ParentID
ItemOrder = tct.ItemOrder
_selected = selected
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class
Although not a VB expert i think i found what is wrong:
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(value))
Should be something like
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Selected"))
I later on confirmed my guess from this msdn link at VB tab