2

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
icecreamsoop
  • 129
  • 5
  • 15

1 Answers1

2

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

iltzortz
  • 2,342
  • 2
  • 19
  • 35
  • Yeah I've tried this also to no avail. Can I ask what you mean by the checkbox not being declared as "threestate"? Where does the 3rd state come from? Nullable? – icecreamsoop Jan 18 '13 at 15:58
  • yes like this: i am not sure which is its default value for IsThreeState true or false – iltzortz Jan 18 '13 at 16:01
  • I've not tired this, I will - but why would I want to? What does this mean? The value being bound to the control is a non-nullable boolean. Thanks! – icecreamsoop Jan 18 '13 at 16:12
  • I just tried this and it didn't work - I see why you suggested it. I'm guessing if the Combo had threestate enabled, but the object was only 2-state, this might have had an effect. I guess not though... I still have no idea. I've removed the event hooks from the XAML too, the values are still correctly populated, when the control is loaded, and the view-model is updated when the boxes are toggled - but it still doesn't update when I change the value of the view-model from elsewhere in the code. – icecreamsoop Jan 18 '13 at 16:17
  • 1
    Thanks for the suggestion - this isn't quite the problem I'm encountering though! When I update the object the checkboxes are bound to in the code-behind, the check-box is not being updated. If I click on the checkbox, it works fine. Thank you for your input anyway! – icecreamsoop Jan 18 '13 at 16:26
  • OK final trial since i see i had misunderstood the problem and now looked closer to code. Check for the last time my answer. If it was c# i would be positive – iltzortz Jan 18 '13 at 16:33