previewmousedown事件

previewmousedown事件

本文介绍了我怎样才能捕获tabitem_previewmousedown事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好!我尝试在我的WPF解决方案中捕获TabItem.PreviewMouseDown,但是当我单击选项卡项时,我将TabControl作为源。我可以在TabItem_PreviewMouseDown事件上给TabItem吗? 我尝试过: 此解决方案运行良好: < tabcontrol name = MyTabControl tabitem.previewmousedown = TabItem_PreviewMouseDown > < tabitem header = Tabitem 0 > &l t; tabitem header = Tabitem 1 > < tabitem header = Tabitem 2 > < tabitem header = Tabitem 3 > < tabitem heade r = Tabitem 4 > < tabitem header = Tabitem 5 > 在main.xaml.cs文件中: private void TabItem_PreviewMouseDown( object sender,MouseEventArgs e) { var tabItem = e.Source as TabItem; // e.Source is TabItem } 我需要设置带有绑定的TabItems: < tabcontrol 名称 = MyTabControl itemssource = {Binding Path = Tabs} tabitem.previewmousedown = TabItem_PreviewMouseDown > in .cs: public ObservableCollection< TA bitem>标签{ get ; set ; } public MainWindow() { InitializeComponent(); Tabs = new ObservableCollection< tabitem>(); for ( int i = 0 ; i < 5 ; i ++) { TabItem mti = new TabItem(){Header = i.ToString()}; Tabs.Add(mti); } DataContext = this ; } private void TabItem_PreviewMouseDown( object sender,MouseEventArgs e) { var tabItem = e.Source as TabItem; // e.Source是TabControl !!! } 如何在TabItem_PreviewMouseDown事件上提供TabItem?解决方案 嗯,你需要手动添加将TabItems添加到集合时的预览处理程序: mti.PreviewMouseDown + = MainWindow_PreviewMouseDown; 当TabControl超出范围时,别忘了手动取消挂钩事件。 再一次,我无法让编译器接受 TabItem.PreviewMouseDown = eventHandler xaml中的 的TabControl Hello! I try to catch TabItem.PreviewMouseDown in my WPF solution, but when I click on tab item I give TabControl as source. can I give TabItem on TabItem_PreviewMouseDown event?What I have tried:This solution working good:<tabcontrol name="MyTabControl" tabitem.previewmousedown="TabItem_PreviewMouseDown"> <tabitem header="Tabitem 0"> <tabitem header="Tabitem 1"> <tabitem header="Tabitem 2"> <tabitem header="Tabitem 3"> <tabitem header="Tabitem 4"> <tabitem header="Tabitem 5">In main.xaml.cs file:private void TabItem_PreviewMouseDown(object sender, MouseEventArgs e){ var tabItem = e.Source as TabItem; // e.Source is TabItem}I need to set TabItems with Binding:<tabcontrol name="MyTabControl" itemssource="{Binding Path=Tabs}" tabitem.previewmousedown="TabItem_PreviewMouseDown">in .cs:public ObservableCollection<tabitem> Tabs { get; set; }public MainWindow(){ InitializeComponent(); Tabs = new ObservableCollection<tabitem>(); for (int i = 0; i < 5; i++) { TabItem mti = new TabItem() { Header = i.ToString() }; Tabs.Add(mti); } DataContext = this;}private void TabItem_PreviewMouseDown(object sender, MouseEventArgs e){ var tabItem = e.Source as TabItem; // e.Source is TabControl!!!}How can I give TabItem on TabItem_PreviewMouseDown event? 解决方案 Well, you need to manually add the preview handler when you add the TabItems to the collection:mti.PreviewMouseDown += MainWindow_PreviewMouseDown;Don't forget to manually unhook those events when the TabControl goes out of scope.Once again, I couldn't get the compiler to acceptTabItem.PreviewMouseDown="eventHandler"in the xaml for the TabControl 这篇关于我怎样才能捕获tabitem_previewmousedown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 23:39