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

1

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.