开发者

Appropriate point to attach NodeChanged event, XAML

开发者 https://www.devze.com 2023-03-16 03:27 出处:网络
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

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 expe开发者_高级运维riences 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号