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 9180355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:01:44+00:00 2026-06-17T18:01:44+00:00

I have a ComboBox with the DataContext defined at application start to the appropriate

  • 0

I have a ComboBox with the DataContext defined at application start to the appropriate ViewModel. I want to grab items from a XML file but have user selections bind to the ViewModel, and ultimately the model.

XAML:

<ComboBox x:Name="cbConnection"
          ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
          DisplayMemberPath="Key"
          SelectedValuePath="Value"
          SelectionChanged="{Binding Path=DataContext.cbConnection_SelectionChanged}"
          />

but I am getting the following exception at runtime:

{"Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'."}

We know the ViewModel is appropriately set as the DataContext of the View’s Window. What am I doing wrong?

  • 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-17T18:01:46+00:00Added an answer on June 17, 2026 at 6:01 pm

    You are using;

    SelectionChanged="{Binding Path=DataContext.cbConnection_SelectionChanged}" 
    

    Which is actually an event.

    You should bind a Public Property (possibly implementing INotifyPropertyChanged) in your ViewModel to the SelectedItem Property to manage changes tn the Selection.

    Assuming that your window has the DataContext, rather than the combobox itself…

    SelectedItem Binding Version:

    So your XAML would be something like;

    <ComboBox x:Name="cbConnection"
              ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
              DisplayMemberPath="Key"
              SelectedValuePath="Value"
              SelectedItem="{Binding Path=DataContext.cbConnectionSelectedItem}"
    />
    

    And in your 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
    

    Text Binding Version:

    Of course, if all your interested in is the Text Value of what they’ve chosen, you could in theory just bind the ComboBox Text Property to a Public String Property in your ViewModel;

    Your XAML would then be;

    <ComboBox x:Name="cbConnection"
                  ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
                  DisplayMemberPath="Key"
                  SelectedValuePath="Value"
                  Text="{Binding Path=DataContext.cbConnectionText}"
        />
    

    And your 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 Binding Version:

    If you are displaying the Key, but want the Value from the Key/Value Pair, then you should bind to the SelectedValue;

    XAML;

    <ComboBox x:Name="cbConnection" 
        ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
        DisplayMemberPath="@Key" 
        SelectedValuePath="@Value" 
        SelectedValue="{Binding Path=DataContext.cbConnectionValue}" />
    

    ViewModel;

    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
    

    Note the extra @ symbols.

    As I mentioned above, this assumes that your Window has the DataContext set here. If not, then remove the “DataContext.”‘s from the Bindings above!

    I assume you’re seeing items listed in your ComboBox currently?

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a combobox from which i need to programmatically disable items depending on
I have a combobox whose value I want to use with a SQL WHERE
I have a combobox in a WPF Window, it is filled with usernames from
I have a combobox in a form and a button. I want to add
I have a combobox that passes along values in text, but they can have
I have some problem to access the Window's DataContext from within a DataGrid. The
I have a combo box defined as such <ComboBox Name=RoomDropDown Visibility={Binding Path=RoomDropDownVisible,Mode=OneWay,Converter={StaticResource BoolVisibilityConvertor}} ItemsSource={Binding
I have a combo box defined as such <ComboBox Name=RoomDropDown Visibility={Binding Path=RoomDropDownVisible,Mode=OneWay,Converter={StaticResource BoolVisibilityConvertor}} ItemsSource={Binding
I have ComboBox that takes data from another class public partial class MainWindow :
I have a combobox that is bound to ObservableCollection<Item> in the ViewModel. When a

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.