如何在WPF中的tabControl的右键单击事件中识别tabPages? [英] How to indentify tabPages in right click event of a tabControl in WPF?

查看:66
本文介绍了如何在WPF中的tabControl的右键单击事件中识别tabPages?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有WPF应用程序.
我有一个选项卡控件.在此选项卡控件中,我有几个选项卡页.当用户右键单击选项卡页面时,将显示一个上下文菜单.该菜单的其中一项是删除".通过单击此项目,应删除右键单击的选项卡页.
现在,问题在于如何识别单击了哪个标签页.在事件处理程序内部,没有证据显示删除了哪个标签页.
请考虑我是WPF的新手.

谢谢

Hi I have a WPF application.
I have a tab control. inside this tab control I have several tab pages. When the user right clicks on a tab page, a context menu appears. one of the items of this menu is "remove". by clicking on this item the right clicked tab page should be removed.
Now, the problem is that how to identify which tab page was clicked. inside the event handler there is no proof of showing which tab page was removed.
please consider that I''m new in WPF.

thanks

private void MenuItem_Remove_Click(object sender, RoutedEventArgs e)
{
//sender does not give us enough information to know which tab page was clicked on
}







The Image[^]

if my WPF source is needed:

<Window x:Class="InfoManager.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">
        <Grid>
      <TabControl Name="tabControlMain" Margin="1,1,48,1">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title}" MouseRightButtonDown="TextBlock_MouseRightButtonDown">
                        <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Remove" Click="MenuItem_Remove_Click">
                                    <MenuItem.Icon>
                                        <Image Width="20" Height="20"  Source="..\Resources\remove.png" Stretch="Fill" />
                                    </MenuItem.Icon>
                                </MenuItem>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                    </TextBlock>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabItem Header="tabItem1" Name="tabItem1">
            </TabItem>
        </TabControl>
        <Button Width="30" Height="30" HorizontalAlignment="Right"  Name="buttonAddTab" VerticalAlignment="Top" Margin="0,5,5,0" Click="buttonAddTab_Click">
        <Image Source="..\resources\add.png" Stretch="UniformToFill"></Image>
        </Button>
    </Grid>
</Window>

推荐答案

* Edit *

再三考虑之后,我的原始答复是错误的,因为用户不必右键单击所选选项卡.我看到了您的困境,但是有一个解决方案(至少有一个解决方案)...

在MenuItem Click处理程序中,发送者是MenuItem.其DataContext将是对提供选项卡项目的数据项的对象的引用.您只需要从向选项卡控件提供数据的集合中删除该对象即可.例如,这是我的测试方法...
*Edit*

After thinking about this again, my original reply was wrong because the user doesn''t necessarily right-click on the selected tab. I see your dilemma, but there''s a solution (well, at least one)...

In the MenuItem Click handler, the sender is a MenuItem. Its DataContext will be a reference to the object providing the data item for the tab item. You just need to remove that object from the collection that is providing data to the tab control. For example, here''s how I tested it...
<UserControl x:Class="WPFTester.TabControlTestPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TabControl x:Name="tabControlMain" Loaded="tabControlMain_Loaded" >
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title}">
                        <TextBlock.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Remove" Click="MenuItem_Remove_Click">
                                    <!--<MenuItem.Icon>
                                        <Image Width="20" Height="20"  Source="..\Resources\remove.png" Stretch="Fill" />
                                    </MenuItem.Icon>-->
                                </MenuItem>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                    </TextBlock>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Content}" />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
</UserControl>


public partial class TabControlTestPage : UserControl
{
    public TabControlTestPage()
    {
        InitializeComponent();
    }


    private TabDataTestList tabDataTestList = new TabDataTestList();

    private void tabControlMain_Loaded(object sender, RoutedEventArgs e)
    {
        tabControlMain.ItemsSource = tabDataTestList;
    }

    private void MenuItem_Remove_Click(object sender, RoutedEventArgs e)
    {
        tabDataTestList.Remove(((sender as MenuItem).DataContext) as TabData);
    }

}


public class TabData
{
    public string Title { get; set; }
    public string Content { get; set; }
}

public class TabDataTestList : ObservableCollection<TabData>
{
    public TabDataTestList()
    {
        this.Add(new TabData() { Title = "Item 1", Content = "Content for item 1" });
        this.Add(new TabData() { Title = "Item 2", Content = "Content for item 2" });
        this.Add(new TabData() { Title = "Item 3", Content = "Content for item 3" });
        this.Add(new TabData() { Title = "Item 4", Content = "Content for item 4" });
        this.Add(new TabData() { Title = "Item 5", Content = "Content for item 5" });
    }
}


这是针对winform的,因为我不确定wpf,但希望这会为您指出正确的方向

this is for winform as im not sure about wpf but hpefully this will point you in the right diretion

protected TabPage ActiveTab
        {
            get
            {
                if (this.tabControl1.SelectedTab != null)
                {
                    return this.tabControl1.SelectedTab;
                }
                return null;
            }
        }



使用只需使用此



to use just use this

TabPage MySelectedTab = ActiveTab;
if (MySelectedTab != null)
{
// do what you need with the active TabPage here
}


我的解决方法如下:

My workaround for this is the following:

public TabItem Clicked;

public void AddTab(string Name)
{
TabControl1.Items.Add(new TabItem{Header = c.Name, Name = c.Name});
((TabItem)TabControl1.Items[TabControl1.Items.Count - 1]).MouseDown += new MouseButtonEventHandler((sender, e) =&gt; { if (e.RightButton == MouseButtonState.Pressed) Clicked = (TabItem)TabControl1.Items[TabControl1.Items.Count - 1]; });
}

//Window Mouse Down Event Handler
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.RightButton == MouseButtonState.Pressed)
        Clicked = null;
}


然后,您可以在任何地方使用Clicked变量来找出Right Clicked TabItem.

祝你好运!


Then you can use the Clicked variable anywhere to find out the Right Clicked TabItem.

Good Luck!


这篇关于如何在WPF中的tabControl的右键单击事件中识别tabPages?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆