我有一个填充了复选框的组合框。
ComboxBox 的 ItemsSource 绑定(bind)到要绑定(bind)复选框的对象列表;一个 View 模型。 View 模型是一个简单的对象(MultiSelectDropDownItem 类型),它有一个 bool 字段名称 Selected。
现在,ItemsSource 已通过编程方式设置。这可以;绑定(bind) View 模型的复选框的属性都已正确填充,如果我选中/取消选中复选框,更改将反射(reflect)在 View 模型中。所以对我来说,双向绑定(bind)是有效的。
问题是当我在其他地方更新这些 MultiSelectDropDownItems 之一的 Selected 属性时。该属性触发 PropertyChanged 事件,但这次更改未反射(reflect)在复选框中。
我已经研究这个很久了,在我的一生中,我无法弄清楚为什么更改没有被更新 - 为什么 PropertyChanged 事件不更新复选框,即使复选框后面有对象它的属性改变了吗?
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
最佳答案
虽然不是 VB 专家,但我想我发现了问题所在:
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(value))
应该是这样的
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Selected"))
后来我从这个msdn link证实了我的猜测在 VB 选项卡
关于wpf - 双向绑定(bind) : Combobox of Checkboxes - Kind of Working but not,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14402259/