0

I have this strange issue I am facing currently.

I have created an WPF application based on WPF page navigation. I have few button and depending on the button click the the user is navigated to respective WPF page.

In these WPF pages I have Tab controls and have used selectionchanged event handler to perform some task.

Now to the issue,

When I try to go a particular page, the selectionchanged event is also executed even before the page is loaded completely, I have tried to use the windows.loaded (based on the answer provided to my previous question - here) - I have no luck.

[I am using WPF Navigation framework]

Somehow the selectionchanged event is executing twice.

How do I stop this from happening?

1
  • Try using snoop, you'll be able to see the origin of the events and who handled them Commented Dec 22, 2012 at 10:07

1 Answer 1

1

I think you should check SelectionChange.AddedItems and SelectionChange.RemovedItems to find the difference between these to firings. I guess that when you select a page, SelectionChange.RemovedItems==0 while when you click on a tabItem to select it, SelectionChange.RemovedItems==1. if so just write:

if (SelectionChange.RemovedItems==0) return; 

Edit1: Please see the first comment.

Edit 2

void tablcontrol_SelectionChange(object sender, SelectionChangedEventArgs e) { if (e.RemovedItems.Count == 0) { // I guess this is the event that happens when a page is selected // in this case just a TabItem is added to the selection // and nothing is removed, so do nothing return; } // if you are here, it means that this is another selection changed event // so Perform those tasks that you mentioned in your question } 
Sign up to request clarification or add additional context in comments.

6 Comments

I guess you mean SelectionChangedEventArgs.AddedItems and RemovedItems. And check the Count property: e.AddedItems.Count != 0 and e.RemovedItems.Count != 0.
@Ron firstly, thank you. Is there any sample code availabe? Also, I am not able to understand as to why this tablcontrol_SelectionChange should trigger when the page loads?
@aioracle SelectionChange happens because Selection Changes; I mean a tab is selected, the one which you will see it content. I try to write some sample code.
@Ron thank you. Another thing which i noticed, I wrote a sample app using only WPF window when I run the app with the breakpoint at SelectionChanged event it is not stopping there. But when I use navigation framework and then run it with the same type of steps above, i get the breakpoint active even before it runs. Thanks will look for your support and sample code on this.
@aioracle your welcome; you should let me know if this answer solved the problem you mentioned in your question or knot. If your comment point out "Another thing", would you please update your question or ask a new question and explain more. thanks.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.