2

I'm not sure what's wrong with the DataGrid I've written. The contents show up correctly but when I try to add in a OnSelectionChanged event handler, sth odd happens.Please help me!

Firstly, no problems below:

<DataGrid ItemsSource="{Binding XPath=services/service}" AutoGenerateColumns="False" Padding="2">
      <DataGrid.Columns>
          <DataGridTextColumn Header=" Service Name " Binding="{Binding XPath=name}" Width="300"/>
          <DataGridTextColumn Header=" Status " Binding="{Binding XPath=status}" />
      </DataGrid.Columns>
</DataGrid>

Here services/service are from my external XML file. I used XmlDataProvider resource in the document. In that XML, some 'services' tag has many 'service' children elements; some 'services' tag does not have any children at all (wondering if this is the cause of problem).

So the resultant UI is some of the datagrids contain all the rows and columns. some of the datagrids only show the headers.

Now I add in this:

<DataGrid ItemsSource="{Binding XPath=services/service}" AutoGenerateColumns="False" Padding="2"
SelectionChanged="DataGrid_SelectionChanged">

And put in a empty method on the code:

private void DataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {


    }

Now I execute, and an exception is thrown

System.NullReferenceException was unhandled Message=Object reference not set to an instance of an object. Source=ForeFront Support Monitor 2 StackTrace: at FSM.MainWindow.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target) in h:\Personal\Visual Studio 2010\Projects\ForeFront Support Monitor 2\ForeFront Support Monitor 2\MainWindow.xaml:line 42 at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter) at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List1 affectedChildren, UncommonField1 templatedNonFeChildrenField) at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)...

Erencie
  • 331
  • 1
  • 3
  • 11
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Duniho Apr 23 '21 at 05:53

1 Answers1

0

XML binding to WPF DataGrid can be torturous.

Try binding XML in a different way (like a object model)... How to bind xml to the WPF DataGrid correctly?

And then check if selection changed event fires correctly?

Community
  • 1
  • 1
WPF-it
  • 18,785
  • 6
  • 51
  • 68
  • Thanks a lot. However, this event handler does not just happen to the DataGrid. This DataGrid is part of the data template of a ListBox element. When I add event handler in other data templates, the same problem also arises. – Erencie Sep 16 '11 at 04:40