将事件处理程序添加到 WPF DataGrid 会引发 NullReferenceException
我不确定我写的 DataGrid 有什么问题。内容显示正确,但是当我尝试添加 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>
这里的服务/服务来自我的外部 XML 文件。我在文档中使用了 XmlDataProvider 资源。在该 XML 中,某些“services”标签具有许多“service”子元素;一些“服务”标签根本没有任何子标签(想知道这是否是问题的原因)。
因此,最终的 UI 是一些包含所有行和列的数据网格。一些数据网格仅显示标题。
现在我添加:
<DataGrid ItemsSource="{Binding XPath=services/service}" AutoGenerateColumns="False" Padding="2"
SelectionChanged="DataGrid_SelectionChanged">
并在代码中添加一个空方法:
private void DataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
}
现在我执行,并抛出异常
System.NullReferenceException 未处理 消息=未将对象引用设置为对象的实例。 来源=ForeFront 支持监视器 2 堆栈跟踪: 在 h:\Personal\Visual Studio 2010\Projects\ForeFront 支持监视器 2\ForeFront 支持监视器 2\MainWindow.xaml 中的 FSM.MainWindow.System.Windows.Markup.IStyleConnector.Connect(Int32 connectionId, Object target):第 42 行 在 System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader,XamlObjectWriter currentWriter) 在 System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) 在 System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject 容器、IComponentConnector componentConnector、IStyleConnector styleConnector、List
1affectedChildren、UncommonField
1 templatedNonFeChildrenField) 在 System.Windows.FrameworkTemplate.LoadContent(DependencyObject 容器,List`1affectedChildren)...
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, UncommonField
1 templatedNonFeChildrenField)
at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XML 绑定到 WPF DataGrid 可能会很痛苦。
尝试以不同的方式绑定 XML(如对象模型)... 如何正确地将 xml 绑定到 WPF DataGrid?
然后检查选择更改事件是否正确触发?
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?