あなたが使用している;
SelectionChanged="{Binding Path=DataContext.cbConnection_SelectionChanged}"
これは実際にイベントです。
選択の変更を管理するには、ViewModel のパブリック プロパティ (おそらく INotifyPropertyChanged を実装する) をSelectedItemプロパティにバインドする必要があります。
コンボボックス自体ではなく、ウィンドウに DataContext があると仮定すると...
SelectedItem バインディング バージョン:
したがって、XAML は次のようになります。
<ComboBox x:Name="cbConnection"
ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
DisplayMemberPath="Key"
SelectedValuePath="Value"
SelectedItem="{Binding Path=DataContext.cbConnectionSelectedItem}"
/>
そしてあなたのViewModelで;
Private _cbConnectionSelectedItem As XmlElement
Public Property cbConnectionSelectedItem As XmlElement
Get
Return _cbConnectionSelectedItem
End Get
Set(value As XmlElement)
If value.Equals(_cbConnectionSelectedItem) = False Then
_cbConnectionSelectedItem = value
OnPropertyChanged("cbConnectionSelectedItem")
End If
End Set
End Property
テキストバインディングのバージョン:
もちろん、彼らが選択したテキスト値だけに関心がある場合は、理論的には、ViewModel で ComboBox Text プロパティを Public String プロパティにバインドするだけで済みます。
XAML は次のようになります。
<ComboBox x:Name="cbConnection"
ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
DisplayMemberPath="Key"
SelectedValuePath="Value"
Text="{Binding Path=DataContext.cbConnectionText}"
/>
そしてあなたのViewModel;
Private _cbConnectionText As String
Public Property cbConnectionText As String
Get
Return _cbConnectionText
End Get
Set(value As String)
If value.Equals(_cbConnectionText) = False Then
_cbConnectionText = value
OnPropertyChanged("cbConnectionText")
End If
End Set
End Property
SelectedValue バインディング バージョン:
キーを表示しているが、キーと値のペアから値が必要な場合は、SelectedValue にバインドする必要があります。
XAML;
<ComboBox x:Name="cbConnection"
ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
DisplayMemberPath="@Key"
SelectedValuePath="@Value"
SelectedValue="{Binding Path=DataContext.cbConnectionValue}" />
ビューモデル;
Private _cbConnectionValue As String
Public Property cbConnectionValue As String
Get
Return _cbConnectionValue
End Get
Set(value As String)
If value.Equals(_cbConnectionText) = False Then
_cbConnectionValue = value
OnPropertyChanged("cbConnectionValue")
End If
End Set
End Property
余分な @ 記号に注意してください。
上で述べたように、これは Window に DataContext がここに設定されていることを前提としています。そうでない場合は、上記のバインディングから「DataContext.」を削除してください。
現在、ComboBox にリストされているアイテムが表示されていると思いますか?
お役に立てれば!