我有一个填充了Checkboxes的ComboBox。
ComboxBox的ItemsSource绑定到要绑定到复选框的对象列表;一个ViewModel。视图模型是一个简单的对象(MultiSelectDropDownItem类型),它有一个布尔字段名称Selected。
现在,ItemsSource以编程方式设置。这可以;绑定到视图模型的复选框的属性都已正确填充,如果选中/取消选中复选框,则更改将反映在视图模型中。所以对我来说,双向绑定正在发挥作用。
问题是我在其他地方更新了其中一个MultiSelectDropDownItems的Selected属性。该属性会触发PropertyChanged事件,但这次更改不会反映在Checkbox中。
我现在已经看了好几年了,对于我的生活,我无法弄清楚为什么更改没有被更新 - 为什么PropertyChanged事件不更新CheckBox,即使是复选框后面的对象它的财产已经改变了吗?
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>
代码隐藏(原谅VB-不是我的选择!):
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
答案 0 :(得分:2)
虽然不是VB专家,但我认为我发现了什么错误:
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(value))
应该像
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Selected"))
我稍后在VB标签
上通过这个msdn link确认了我的猜测