Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将事件处理程序添加到WPF DataGrid会引发NullReferenceException_Wpf_Xaml_Wpf Controls_Wpfdatagrid_Xmldataprovider - Fatal编程技术网

将事件处理程序添加到WPF DataGrid会引发NullReferenceException

将事件处理程序添加到WPF DataGrid会引发NullReferenceException,wpf,xaml,wpf-controls,wpfdatagrid,xmldataprovider,Wpf,Xaml,Wpf Controls,Wpfdatagrid,Xmldataprovider,我不确定我写的数据网格有什么问题。内容显示正确,但当我尝试添加OnSelectionChanged事件处理程序时,出现了一些奇怪的情况。请帮助我 首先,没有以下问题: <DataGrid ItemsSource="{Binding XPath=services/service}" AutoGenerateColumns="False" Padding="2"> <DataGrid.Columns> <DataGridTextColum

我不确定我写的数据网格有什么问题。内容显示正确,但当我尝试添加OnSelectionChanged事件处理程序时,出现了一些奇怪的情况。请帮助我

首先,没有以下问题:

<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>
现在我执行,并抛出一个异常

System.NullReferenceException未处理 Message=对象引用未设置为对象的实例。 来源=前线支持监视器2 堆栈跟踪: 在h:\Personal\Visual Studio 2010\Projects\ForeFront Support Monitor 2\ForeFront Support Monitor 2\MainWindow.xaml中的FSM.MainWindow.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId,对象目标)中 位于System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader,XamlObjectWriter currentWriter) 位于System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) 在System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject容器、IComponentConnector componentConnector、IStyleConnector styleConnector、List
1 affectedChildren、Unmonfield
1 templatedNonFeChildrenField) 在System.Windows.FrameworkTemplate.LoadContent(DependencyObject容器,列表'1 affectedChildren')


与WPF DataGrid的XML绑定可能会带来痛苦

尝试以不同的方式绑定XML(如对象模型)


然后检查selection changed事件是否正确触发?

绑定到WPF DataGrid的XML可能会很麻烦

尝试以不同的方式绑定XML(如对象模型)


然后检查选择更改事件是否正确触发?

非常感谢。但是,此事件处理程序不仅仅发生在DataGrid上。此DataGrid是ListBox元素的数据模板的一部分。当我在其他数据模板中添加事件处理程序时,同样的问题也会出现。非常感谢。但是,此事件处理程序不仅仅发生在DataGrid上。此DataGrid是ListBox元素的数据模板的一部分。当我在其他数据模板中添加事件处理程序时,同样的问题也会出现。这是否回答了您的问题?这回答了你的问题吗?
<DataGrid ItemsSource="{Binding XPath=services/service}" AutoGenerateColumns="False" Padding="2"
SelectionChanged="DataGrid_SelectionChanged">
private void DataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {


    }