0

I want to use SelectedItem to set selection to a combobox from code. I can only get it to work by using SelectedValue. SelectedItem will throw a null reference exception with this at the top of the stacktrace:

at AttachedCommandBehavior.CommandBehaviorBinding.Execute()

The 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>

The code:

Imports AttachedCommandBehavior

Public Class 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

End Class

Why does selecteditem not work?

UPDATE:

Nikolay: you have a keen eye. That was due to last minute copy paste work!

Blindmeis: this, ofcourse, is an abstract from a much larger program in which I need the selectionchanged event to execute some actions. Those commandbindings have to stay (though maybe they need some fixing).

Regards,

Michel

Michel van Engelen
  • 2,791
  • 2
  • 29
  • 45
  • Try changing the commented out line to `SelectedValue = 2` I have a feeling its your event binding on the SelectionChanged – Jason Ridge Apr 20 '12 at 10:55
  • when ever your seleteditem change the the selection change so you can all handle in your vm without commanding? or do i miss something? – blindmeis Apr 20 '12 at 11:02
  • ExitMusic: I want it to work with SelectedItem and I'm demonstrating that SelectedValue works but SelectedItem not. SelectedItem is used in ComboItems2. – Michel van Engelen Apr 20 '12 at 11:03
  • Blindmeis, correct: the commandbinding let's me handle the selectionchanged event directly in the viewmodel without having to do plumbing in the view. – Michel van Engelen Apr 20 '12 at 11:06

2 Answers2

3

why you have these commandbindings?

    <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 ;)

this works.

Edit:

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

         }
     }      
blindmeis
  • 22,175
  • 7
  • 55
  • 74
1
acb:CommandBehavior.CommandParameter="{Binding ElementName=ComboItems, Path=SelectedItem}"

You don't have element with name ComboItems, you have ComboItems1 and ComboItems2. I think this is the problem.

Nikolay
  • 3,658
  • 1
  • 24
  • 25