2017-06-07 176 views
1

我想一個組合框的的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) 
+0

*「我試圖做的的SelectedItem返回Selection屬性類似」 * - 請分享您所嘗試的完整代碼,以及它究竟如何失敗。 「類似的東西」和「它不起作用」不足以讓任何人猜測你做錯了什麼。 –

+1

@EdPlunkett我的壞。我更新了我的問題。 –

回答

1

您應該讓XAML爲您做更多的辛苦工作。讓您的CustomItem一次,並綁定到其屬性沒有任何大驚小怪左右。

<Border DataContext="{Binding SourceList[10]}"> 
    <ComboBox 
     DisplayMemberPath="Desc" 
     ItemsSource="{Binding Source}" 
     SelectedItem="{Binding Selection}" 
     /> 
</Border> 

這是什麼意思。讓我們呼籲SourceList[10]itemX

var itemX = SourceList[10]; 
  • 組合框將顯示CustomValueitemX.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> 
+0

第一個示例中使用的索引器不會在索引= 10的列表中獲取項目嗎? –

+0

@FabioL。我會更新以澄清。 –

+0

@FabioL。查看更新。我猜對於您選擇的項目想要做什麼錯了嗎? –