Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:33:16+00:00 2026-06-02T13:33:16+00:00

I want to use SelectedItem to set selection to a combobox from code. I

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:33:18+00:00Added an answer on June 2, 2026 at 1:33 pm

    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
    
             }
         }      
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want use some data from a website with web service. i have a
I am filling items to my ComboBox from a XML file using a DataTable.
Can I use a trigger on the SelectedItem property in any control that supports
I have the following code that I'm trying to use to populate a ComboBox,
I want to use this query: var queryData = from va in xdoc.Descendants(language) select
I want use <p:messages> to display error message, use <p:growl> to display success messages.
I want use groovy findAll with my param to filtering closure filterClosure = {
I want use JQuery mobile for the front-end of my mobile application, but I
I want use jQuery in my project. I know the javascript_include_tag calls the jQuery
I have to use boost::spirit for parsing, and I want use phrase_parse function :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.