首页 新闻 会员 周边 捐助

WPF 路由事件 同一种控件嵌套该怎么办

0
悬赏园豆:20 [待解决问题]

假设我的画面上有一个TabControl控件,有2个TabItem,没个TabItem里面又包含一个TabControl控件,然后给所有的TabControl控件都绑定一个SelectionChanged事件。

现在我点外层的tabitem切换,里面的TabControl也会触发SelectionChanged事件。求大神帮助。

 

网上介绍的例子都是tabcontrol里面放datagrid的例子,没有放相同控件的例子。。。。

V587的主页 V587 | 初学一级 | 园豆:15
提问于:2014-08-25 14:59
< >
分享
所有回答(3)
0

把里面的tabcontrol的事件selectionchanged的路由支持删除就好了。

具体的禁用方法需要代码,我不方便。

此外,可以对子tabcontrol进行识别,对子tabcontrol的selectionchanged事件的e的属性配置下,可以不再路由冒泡,具体的属性你查下。

519740105 | 园豆:5810 (大侠五级) | 2014-08-25 15:42

路由支持删除和进行识别都不行,因为实际代码中,我这边只能确定的就是外层的这个tabcontrol,里层加几个,具体有多少层的tabcontrol 都是动态加载的。谢谢

支持(0) 反对(0) V587 | 园豆:15 (初学一级) | 2014-08-25 16:38

@V587: 那你识别外层的,如果不是外层的,就在事件处理后取消冒泡。

最好是对里层的tabcontrol与外层的tabcontrol使用不同的事件处理程序。

支持(0) 反对(0) 519740105 | 园豆:5810 (大侠五级) | 2014-08-25 16:41
0

e.handled = true

p.yl | 园豆:199 (初学一级) | 2014-08-25 16:16

这个设置没用,我试了

支持(0) 反对(0) V587 | 园豆:15 (初学一级) | 2014-08-25 16:34
0

这个你没办法解决,这是wpf里面自带的bug,如果用了TabControl 容器中的ComboBox改变时都会触发TabControl的 SelectionChanged。所以只有通过代码来进行控制,参考代码如下

private void Window_Loaded(object sender, RoutedEventArgs e)
{
isLoaded = 1;

}

private void tcPatienc_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
//FileHelp.WriteFile(filePath, "tcPatienc_SelectionChanged");
if (e.OriginalSource is TabControl)
{
if (isLoaded != 1)
{
// FileHelp.WriteFile(filePath, "isLoaded != 1");
_loading.Visibility = Visibility.Collapsed;
return;
}
if (e.AddedItems.Count > 0 && e.AddedItems[0] is HeaderedContentControl)
{
//FileHelp.WriteFile(filePath, "e.AddedItems[0] is HeaderedContentControl");
Thread td = new Thread(new ThreadStart(() =>
{
//FileHelp.WriteFile(filePath, "Thread");
Application.Current.Dispatcher.Invoke(DispatcherPriority.Send, new Action(() =>
{
//FileHelp.WriteFile(filePath, "_loading.Visibility = Visibility.Visible;");
_loading.Visibility = Visibility.Visible;
}));
Application.Current.Dispatcher.Invoke(DispatcherPriority.SystemIdle, new Action(() =>
{
// FileHelp.WriteFile(filePath, "switch (Header)");

string Header = ((System.Windows.Controls.HeaderedContentControl)(((object[])(e.AddedItems))[0])).Header.ToString();
//FileHelp.WriteFile(filePath, Header);
switch (Header)
{
case "编辑患者":
btNew.Visibility = Visibility.Collapsed;
break;
case "住院信息":
// FileHelp.WriteFile(filePath, "执行前" + Header);
btNew.Visibility = Visibility.Visible;
scroll.Content = hospitaInformation;
// FileHelp.WriteFile(filePath, " scroll.Content" + Header);
tiHospitaIinformation.Content = scroll;
// FileHelp.WriteFile(filePath, "tiHospitaIinformation.Content" + Header);
break;
case "临床诊断":
// FileHelp.WriteFile(filePath, "执行前" + Header);
btNew.Visibility = Visibility.Visible;
scroll1.Content = diagnosisInfo;
// FileHelp.WriteFile(filePath, " scroll.Content" + Header);
tiDiagnosisInfo.Content = scroll1;
//FileHelp.WriteFile(filePath, "tiDiagnosisInfo.Content" + Header);
break;
case "影像学检查":
btNew.Visibility = Visibility.Visible;
scroll2.Content = imageCheckFrom;
tiImageCheckFrom.Content = scroll2;
break;
case "术前诊断":
btNew.Visibility = Visibility.Visible;
scroll3.Content = pdFrom;
tiPreoperativeDiagnosisFrom.Content = scroll3;
break;
case "手术信息":
btNew.Visibility = Visibility.Visible;
scroll4.Content = oiFrom;
tiOperationInformationFrom.Content = scroll4;
break;
case "出院前评估":
btNew.Visibility = Visibility.Visible;
scroll5.Content = deFrom;
tiDischargeEvaluationFrom.Content = scroll5;
break;
//case "术后治疗":
// btNew.Visibility = Visibility.Visible;
// scroll6.Content = srFrom;
// tiSurgeryRadiotherapyFrom.Content = scroll6;
// break;
case "术后随访":
btNew.Visibility = Visibility.Visible;
scroll7.Content = sfFrom;
tiSurgeryFollowFrom.Content = scroll7;
break;
case "术后治疗":
btNew.Visibility = Visibility.Visible;
scroll8.Content = surgeryNew;
SurgeryNew.Content = scroll8;
break;
case "课题管理":
btNew.Visibility = Visibility.Visible;
scroll9.Content = subjectPatient;
tiSubjectPatient.Content = scroll9;
break;
}
}));
// FileHelp.WriteFile(filePath, "switch (Header)结束");
Application.Current.Dispatcher.Invoke(DispatcherPriority.Send, new Action(() =>
{
_loading.Visibility = Visibility.Collapsed;
// FileHelp.WriteFile(filePath, "_loading.Visibility = Visibility.Collapsed;");
}));
}));
//FileHelp.WriteFile(filePath, "Thread开始");
td.IsBackground = true;
td.Start();
FileHelp.WriteFile(filePath, "Thread开始启动完毕");
}
else { return; }
}
else
{
_loading.Visibility = Visibility.Collapsed;
//FileHelp.WriteFile(filePath, "_loading.Visibility = Visibility.Collapsed;");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//FileHelp.WriteFile(filePath, "ex.Message");
}

}

沦落的伤心人 | 园豆:208 (菜鸟二级) | 2014-09-02 14:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册