Appropriate point to attach NodeChanged event, XAML

五迷三道 提交于 2019-12-11 06:18:35

问题


I've got a WPF usercontrol in a winforms application form.

Basically, I want a generic eventhandler attached to my WPF TreeView to handle "Document.NodeChanged". As this particular event fires when the tree is populated, I attempted to do a late attachment, via my treeview control's Loaded event.

The code goes something like:

private void UpdateGrid()
{
    myGridView.UpdateXML(entityId, runDate, rtbToggleFullView.ToggleState == Telerik.WinControls.Enumerations.ToggleState.Off, userName);
    //Safely attach the event to fire when the treeview has finished loading.
    myGridView.tvRatings.Loaded -= AttachNodeChangedEvent;
    myGridView.tvRatings.Loaded += AttachNodeChangedEvent;
}

Then the "AttachNodeChangedEvent" method looks like this:

public void AttachNodeChangedEvent(object i, EventArgs a)
{
    ((XmlDataProvider)myGridView.dataProvider).Document.NodeChanged -= OnNodeChanged;
    ((XmlDataProvider)myGridView.dataProvider).Document.NodeChanged += OnNodeChanged;
}

With a simple OnNodeChanged method:

public void OnNodeChanged(object i, EventArgs a)
{
  Dirty = true;
}

The idea being:-

  1. UpdateGrid runs UpdatesXML on treeview
  2. Attaches a "NodeChangeHandler attacher" to treeview.Loaded
  3. (when treeview is loaded) treeview fires "NodeChangeHandler attacher" which then attaches "OnNodeChanged" to the Treeview's populated XmlDocumentProvider.

This appeared to work perfectly in Windows 7. In Windows XP, however, the AttachNodeChangedEvent routine fires, and experiences a NullReferenceException (presumably because the Document hasn't loaded yet?) crashing the app.

Commenting out the ...((XmlDataProvider)myGridView... lines fixes the crash, but obviously disables the functionality.

Can anyone suggest a better way of achieving the same, or shed some light on why this works for Windows 7, but not Windows XP? "e.g. Attach NodeChangedEvent after the initial population of the Treeview"

I can confirm that both use the appropriate .Net Framework 4 package and seem to have all other dependencies appropriately included.

Thanks!

来源:https://stackoverflow.com/questions/6494806/appropriate-point-to-attach-nodechanged-event-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!