WPF组合框的SelectedItem动态绑定
我想一个组合框的的SelectedItem绑定到视图模型里面一个ObservableCollection的具体项目。WPF组合框的SelectedItem动态绑定
在视图模型我有一个的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>
转换器返回在组合框中选择了正确的值,但是这样一来就会提高对SOURCELIST属性设置方法,因为它是一个声明为第一路径在多重绑定中。
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)
您应该让XAML为您做更多的辛苦工作。让您的CustomItem
一次,并绑定到其属性没有任何大惊小怪左右。
<Border DataContext="{Binding SourceList[10]}">
<ComboBox
DisplayMemberPath="Desc"
ItemsSource="{Binding Source}"
SelectedItem="{Binding Selection}"
/>
</Border>
这是什么意思。让我们呼吁SourceList[10]
itemX
:
var itemX = SourceList[10];
组合框将显示
CustomValue
在itemX.Source
中发现的集合。组合框将其选定的项目分配给
itemX.Selection
。
或者更好的是,这个。我不能肯定这是你在做什么的情况下,但它说明去想这个东西的方式:
<ItemsControl ItemsSource="{Binding SourceList}">
<ItemsControl.ItemsTemplate>
<DataTemplate>
<!--
Inside the DataTemplate, the DataContext will be one item
from SourceList.
-->
<Border>
<ComboBox
DisplayMemberPath="Desc"
ItemsSource="{Binding Source}"
SelectedItem="{Binding Selection}"
/>
</Border>
</DataTemplate>
</ItemsControl.ItemsTemplate>
</ItemsControl>
第一个示例中使用的索引器不会在索引= 10的列表中获取项目吗? –
@FabioL。我会更新以澄清。 –
@FabioL。查看更新。我猜对于您选择的项目想要做什么错了吗? –
*“我试图做的的SelectedItem返回Selection属性类似” * - 请分享您所尝试的完整代码,以及它究竟如何失败。 “类似的东西”和“它不起作用”不足以让任何人猜测你做错了什么。 –
@EdPlunkett我的坏。我更新了我的问题。 –