2012-04-20 18 views
0

SelectedItemを使用してコードからコンボボックスに選択範囲を設定したいと考えています。 私はSelectedValueを使って動作させることしかできません。 AttachedCommandBehavior.CommandBehaviorBinding.ExecuteでSelectedItem on combobox null参照例外

()

XAML:

<Window x:Class="MainWindowView" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:acb="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior" 
Title="Window1" Height="300" Width="300"> 
<StackPanel> 
    <ComboBox Name="ComboItems1" 
       DisplayMemberPath="Value" 
       SelectedValuePath="Key" 
       ItemsSource="{Binding Items}" 
       SelectedValue="{Binding SelectedValue}" 
       acb:CommandBehavior.Event="SelectionChanged" 
       acb:CommandBehavior.Command="{Binding Path=SelectionChangedCommand}" 
       acb:CommandBehavior.CommandParameter="{Binding ElementName=ComboItems1, Path=SelectedItem}" /> 

    <ComboBox Name="ComboItems2" 
       DisplayMemberPath="Value" 
       SelectedValuePath="Key" 
       ItemsSource="{Binding Items}" 
       SelectedItem="{Binding SelectedItem}" 
       acb:CommandBehavior.Event="SelectionChanged" 
       acb:CommandBehavior.Command="{Binding Path=SelectionChangedCommand}" 
       acb:CommandBehavior.CommandParameter="{Binding ElementName=ComboItems2, Path=SelectedItem}"/> 
</StackPanel> 

コードのSelectedItemは、スタックトレースの上部にこれにnull参照例外がスローされます:

Imports AttachedCommandBehavior 

パブリック・クラスMainWindowViewModel

Private _mainWindowView As MainWindowView 

Public Property Items As New List(Of KeyValuePair(Of Integer, String)) 
Public Property SelectedItem As Nullable(Of KeyValuePair(Of Integer, String)) 
Public Property SelectedValue As Nullable(Of Integer) 
Public Property SelectionChangedCommand As ICommand 

Public Sub New() 

    Items.Add(New KeyValuePair(Of Integer, String)(1, "first item")) 
    Items.Add(New KeyValuePair(Of Integer, String)(2, "second item")) 
    Items.Add(New KeyValuePair(Of Integer, String)(3, "third item")) 

    Dim simpleCommand As SimpleCommand = New SimpleCommand() 
    simpleCommand.ExecuteDelegate = Sub(selectedItem As Object) 
             HandleSelectionChanged(selectedItem) 
            End Sub 
    SelectionChangedCommand = simpleCommand 

    SelectedValue = 1 
    'SelectedItem = Items(1) 'uncomment this to raise the null ref exception 

End Sub 

Private Sub HandleSelectionChanged(ByRef selectedItem As Object) 
    If selectedItem IsNot Nothing Then 
     'Do something 
    End If 
End Sub 

エンドクラス

なぜのSelectedItemが動作しませんか?

UPDATE:

ニコライ:あなたは鋭い目を持っています。それは直前のコピー貼り付け作業によるものです!

Blindmeis:これはもちろん、いくつかのアクションを実行するには、私がselectionchangedイベントが必要な、より大きなプログラムの要約です。それらのコマンドバインディングは(いくつかの修正が必要かもしれないが)残らなければならない。

よろしく、

ミシェル

+0

'SelectedValueのは= 2'私が今まであなたseleteditemが選択変更を変更したときSelectionChanged –

+0

に結合そのあなたのイベントを感じているにコメントアウト行を変更してみてくださいあなたは指揮せずにあなたのVMにすべて対応できますか?または私は何かが恋しいですか? – blindmeis

+0

ExitMusic:SelectedItemで動作させたいのですが、SelectedValueは動作していますがSelectedItemは動作していないことを示しています。 SelectedItemはComboItems2で使用されます。 –

答えて

3

なぜこれらのコマンドバインディングがありますか?

<ComboBox 
      DisplayMemberPath="Value" 
      SelectedValuePath="Key" 
      ItemsSource="{Binding Items}" 
      SelectedItem="{Binding SelectedItem}" /> 

のviewmodel

//this select the "third item" in your combobox 
    SelectedItem = Items[2];/dont know the vb indexer stuff ;) 

これは動作します。

編集:

のviewmodel

 public KeyValuePair<int, string> SelectedItem 
    { 
     get{return this._selectedItem;} 
     set{ 

      if(this._selectedItem==value) 
       return;//no selection change 

      //if you got here then there was a selection change 
      this._selectedItem=value; 
      this.OnPropertyChanged("SelectedItem"); 
      //do all action you want here 
      //and you do not need selection changed event commmandbinding stuff 

     } 
    }  
+0

私はそれについて考えていない、それは確かに受け入れられます。私はセッターからイベントを起こす。 誰かが最初の問題がなぜ発生するのか知っていたら、私に教えてください:)。 –

+0

あなたはすでにセッターでイベントを発生させています:) INotifyPropertyChanged! – blindmeis

1
acb:CommandBehavior.CommandParameter="{Binding ElementName=ComboItems, Path=SelectedItem}" 

はあなたがComboItems1とComboItems2を持って、名前ComboItemsを持つ要素を持っていません。私はこれが問題だと思う。