如何使用标题中的按钮更改 WPF TabControl 中按钮单击的选定选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1075139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:39:54  来源:igfitidea点击:

How to change selected tab on Button click in WPF TabControl with Buttons in Header

wpftabcontrolselecteditem

提问by HiteshP

I have a WPF TabControlthat has a couple of buttons in the TabItemheader. I want the selected tab to change when a headered button is clicked. Here is a code snippet:

我有一个 WPF TabControl,在TabItem标题中有几个按钮。我希望在单击标题按钮时更改所选选项卡。这是一个代码片段:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   <Grid>
      <TabControl>
         <TabItem Content="Item 1 Content">
            <TabItem.Header>
               <StackPanel Orientation="Vertical">
                  <TextBlock FontSize="14" FontWeight="Bold" Text="Item1"/>
                  <StackPanel Orientation="Horizontal">
                     <Button Content="Action 1"/>
                     <Button Content="Action 2"/>
                  </StackPanel>
               </StackPanel>
            </TabItem.Header>
         </TabItem>
         <TabItem Content="Item 2 Content">
            <TabItem.Header>
               <StackPanel Orientation="Vertical">
                  <TextBlock FontSize="14" FontWeight="Bold" Text="Item2"/>
                  <StackPanel Orientation="Horizontal">
                     <Button Content="Action 1"/>
                     <Button Content="Action 2"/>
                  </StackPanel>
               </StackPanel>
            </TabItem.Header>
         </TabItem>
      </TabControl>
   </Grid>
</Page>

This sample show a couple of Tabs. A tab is selected if the header background is clicked, however, if a button is clicked the tab is not selected. I want the button to accept the click but I also want the tab corresponding to the button to get selected. Can anyone help?

此示例显示了几个Tabs。如果单击标题背景,则会选择选项卡,但是,如果单击按钮,则不会选择选项卡。我希望按钮接受点击,但我也希望与按钮对应的选项卡被选中。任何人都可以帮忙吗?

Thanks, Hitesh

谢谢,希特什

采纳答案by rmoore

We can do this by using Event Routing. RoutedEvents, such as Click will bubble up the element tree, until something handles the event. Because of this, you're actually already receiving the Click event on the tab items, we just aren't doing anything with it yet. We could create an event to handle the Button Click on the tab items like this:

我们可以通过使用事件路由来做到这一点。 RoutedEvents,例如 Click 将在元素树中冒泡,直到某些东西处理该事件。因此,您实际上已经在选项卡项目上接收到 Click 事件,我们只是还没有对它做任何事情。我们可以创建一个事件来处理按钮单击选项卡项目,如下所示:

<TabItem Content="Item 1 Content" ButtonBase.Click="TabItem_Click">

However, we'd have to set that on each tab, so instead we can create a style for the TabItems in the TabControl like so:

但是,我们必须在每个选项卡上设置它,因此我们可以为 TabControl 中的 TabItems 创建一个样式,如下所示:

<TabControl>
    <TabControl.ItemContainerStyle>
        <Style TargetType="{x:Type TabItem}">
            <EventSetter Event="ButtonBase.Click"
                         Handler="TabItem_Click" />
        </Style>
    </TabControl.ItemContainerStyle>
....
</TabControl>

Now, in our event handler we can select the tab that has been clicked:

现在,在我们的事件处理程序中,我们可以选择已单击的选项卡:

private void TabItem_Click(object sender, RoutedEventArgs e)
{
    Trace.WriteLine("TabItemClicked");
    ((TabItem)sender).IsSelected = true;
    e.Handled = true;
}

回答by dhaval

I was doing a little RnD on the above problem right now and was able to achieve the above in a different way but still it would be great if u could help me in the way u hv executed.

我现在正在对上述问题进行一些研究,并且能够以不同的方式实现上述目标,但如果您能以您执行的方式帮助我,那仍然会很棒。

On the selectionchanged event of the listbox I just changed the selecteditem of the tab control to the one i want i.e.

在列表框的 selectionchanged 事件上,我只是将选项卡控件的 selecteditem 更改为我想要的那个

            Tbctrl.SelectedItem = (TabItem)Tbctrl.FindName("item2");

Here Tbctrl is the name of the tabcontrol and item2 is the name of the tabitem in the tabcontrol which contains the textboxes mentioned above.

这里 Tbctrl 是 tabcontrol 的名称,item2 是 tabcontrol 中包含上述文本框的 tabitem 的名称。

Regards,

问候,

Dhaval

达瓦尔