我想一個組合框的的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)
*「我試圖做的的SelectedItem返回Selection屬性類似」 * - 請分享您所嘗試的完整代碼,以及它究竟如何失敗。 「類似的東西」和「它不起作用」不足以讓任何人猜測你做錯了什麼。 –
@EdPlunkett我的壞。我更新了我的問題。 –