0

我目前面临这个奇怪的问题。

我创建了一个基于 WPF 页面导航的 WPF 应用程序。我有几个按钮,根据按钮单击,用户将导航到相应的 WPF 页面。

在这些 WPF 页面中,我有 Tab 控件并使用 selectionchanged 事件处理程序来执行某些任务。

现在谈到这个问题,

当我尝试转到特定页面时,甚至在页面完全加载之前也会执行 selectionchanged 事件,我尝试使用 windows.loaded (基于对我之前的问题提供的答案 -这里) - 我没有运气.

[我正在使用 WPF 导航框架]

不知何故, selectionchanged 事件执行了两次。

我该如何阻止这种情况发生?

4

1 回答 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
}
于 2012-12-22T09:52:50.233 回答