tags:

views:

171

answers:

2

Hi,

In my app, I have a group of 3d objects and they're exposed to the user through a TreeView. When a user selects an item in the TreeView, an SelectedItemChanged event is fired, the corresponding 3d object is set to be selected and is highlighted in the 3d render window. This works fine.

What I'm having trouble with is the reverse. In a section of my code, I programatically set the selected 3d object in the scene. I want to reflect the currently selected object in the TreeView, so I run through the items until I find the corresponding one. But once I get to it, I can't find a way to make the item appear selected without having SelectedItemChanged being called, which is not what I want.

Is there a way to do this?

Thanks!

+3  A: 

I take it you want to suppress the code in your event-handler? If so, a common way of doing this is with a boolean flag (or sometimes an int counter):

bool updatingSelected;

void SomeHandler(object sender, EventArgs args) { // or whatever
  if(updatingSelected) return;

  //...
}

void SomeCode() {
    bool oldFlag = updatingSelected;
    updatingSelected = true;
    try {
       // update the selected item
    } finally {
       updatingSelected = oldFlag;
    }
}
Marc Gravell
Hi Marc!That was my first idea, but since I might have many controls that should behave similarly, it might not scale very well. I was hoping to avoid a solution like that, but I might have to grin and bear it.
Steve the Plant
This is just the way that I'd do it. I could be wrong, but I don't see any scale problem. You just need the one flag--or perhaps one for each control that you want to be non-responsive, in case of cascades--that you have to remember to set ever time you write a "code selects the control" call.
RolandTumble
A: 

Would it be appropriate to remove the TreeView's SelectedItemChanged event handler temporarily, and re-add it once you've performed the necessary operations? I haven't tried it myself, but it's the only other thing I can think of (Marc Gravell beat me to my original answer - I've done THAT before ;) ).

Good luck!

Pwninstein