【发布时间】:2017-11-09 03:47:48
【问题描述】:
我想将 ComboBox 的 SelectedItem 绑定到 ViewModel 内 ObservableCollection 的特定项。
在 ViewModel 中,我有一个 ObservableCollection 属性:
Public Property SourceList As ObservableCollection(Of CustomItem)
那我有这两个自定义类
Public Class CustomItem
Public Property Code As String
Public Property Source As List(Of CustomValue)
Public Property Selection As Object
End Class
Public Class CustomValue
Public Property Id As Integer
Public Property Desc As String
End Class
XAML 中的组合框
<Border Tag="10">
<ComboBox DisplayMemberPath="Desc">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource comboSourceConverter}">
<Binding Path="SourceList"/>
<Binding Path="Tag" RelativeSource="{RelativeSource AncestorLevel=1, Mode=FindAncestor, AncestorType=Border}" />
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
</Border>
我用来设置ItemsSource的转换器,工作正常。
Public Function Convert(values() As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IMultiValueConverter.Convert
If values(0) IsNot Nothing AndAlso values(1) IsNot Nothing Then
Return DirectCast(values(0), ObservableCollection(Of CustomItem)).Where(Function(x) x.Code = CStr(values(1))).First().Source
End If
Return Binding.DoNothing
End Function
Public Function ConvertBack(value As Object, targetTypes() As Type, parameter As Object, culture As CultureInfo) As Object() Implements IMultiValueConverter.ConvertBack
Throw New NotImplementedException
End Function
我尝试为 SelectedItem 做类似的事情。 在 XAML 中:
<Border Tag="10">
<ComboBox DisplayMemberPath="Desc">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource comboSourceConverter}">
<Binding Path="SourceList"/>
<Binding Path="Tag" RelativeSource="{RelativeSource AncestorLevel=1, Mode=FindAncestor, AncestorType=Border}" />
</MultiBinding>
</ComboBox.ItemsSource>
<ComboBox.SelectedItem>
<MultiBinding Converter="{StaticResource comboSourceConverter}">
<Binding Path="SourceList"/>
<Binding Path="Tag" RelativeSource="{RelativeSource AncestorLevel=1, Mode=FindAncestor, AncestorType=Border}" />
</MultiBinding>
</ComboBox.SelectedItem>
</ComboBox>
转换器返回在 ComboBox 中选择的正确值,但这样会引发 SourceList 属性的 Set 方法,因为它是在多重绑定中声明为第一个 Path 的方法。
Public Function Convert(values() As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IMultiValueConverter.Convert
If values(0) IsNot Nothing AndAlso values(1) IsNot Nothing Then
Return DirectCast(values(0), ObservableCollection(Of CustomItem)).Where(Function(x) x.Code = CStr(values(1))).First().Selection
End If
Return Binding.DoNothing
End Function
Public Function ConvertBack(value As Object, targetTypes() As Type, parameter As Object, culture As CultureInfo) As Object() Implements IMultiValueConverter.ConvertBack
Return New Object(){value}
End Function
手动设置绑定一切正常,但如果可能,我更喜欢通过转换器完成所有工作。
Dim _item As CustomItem = SourceList.Where(Function(x) x.Code = Border.Tag).FirstOrDefault()
Dim _binding = New Binding("Selection")
_binding.Mode = BindingMode.TwoWay
_binding.Source = _item
ComboBox.SetBinding(ComboBox.SelectedItemProperty, _binding)
【问题讨论】:
-
“我尝试为返回 Selection 属性的 SelectedItem 做类似的事情”——请分享您尝试过的完整代码,以及它是如何失败的。 “类似的东西”和“它不起作用”不足以让任何人猜测你做错了什么。
-
@EdPlunkett 我的错。我更新了我的问题。
标签: wpf xaml combobox binding converter