C#WPF如何在TabControl.SelectionChanged()上将控件从一个TabItem移动到另一个TabItem? [英] C# WPF How do I move a control from one TabItem to another on TabControl.SelectionChanged()?

查看:66
本文介绍了C#WPF如何在TabControl.SelectionChanged()上将控件从一个TabItem移动到另一个TabItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写一个WPF应用程序,它有一个包含四个选项卡的WPF TabControl。必须将一个GroupBox控件移动到TabControl的SelectionChanged()事件中新选择的TabItem。在这个应用程序的旧C#Winforms版本中,
中的SelectedIndexChanged()事件的代码就像这样......

I'm writing a WPF app in C# that has a WPF TabControl containing four tabs. There is a GroupBox control that must be moved to the newly selected TabItem in the TabControl's SelectionChanged() event. In an old C# Winforms version of this app, the code in the SelectedIndexChanged() event went like this...

groupBox1.Parent = tabControl1.SelectedTab

足够简单。我找不到任何在WPF中如何执行此操作的代码示例。或者甚至可以在WPF中使用?

Simple enough. I can't find any code examples of how to do this in WPF. Or is this even possible in WPF?

推荐答案


我正在用C#编写一个WPF应用程序,它有一个包含四个选项卡的WPF TabControl。必须将一个GroupBox控件移动到TabControl的SelectionChanged()事件中新选择的TabItem。在这个应用程序的旧C#Winforms版本中,
中的SelectedIndexChanged()事件的代码就像这样......

I'm writing a WPF app in C# that has a WPF TabControl containing four tabs. There is a GroupBox control that must be moved to the newly selected TabItem in the TabControl's SelectionChanged() event. In an old C# Winforms version of this app, the code in the SelectedIndexChanged() event went like this...

groupBox1.Parent = tabControl1.SelectedTab

足够简单。我找不到任何在WPF中如何执行此操作的代码示例。或者甚至可以在WPF中使用它?

Simple enough. I can't find any code examples of how to do this in WPF. Or is this even possible in WPF?

嗨    WikiGrrrl,



您可以通过Element.Children属性执行此操作:



以下代码供你参考。

Hi   WikiGrrrl,

You can do this via the Element.Children property:

The following code for your reference.

         <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="371*"/>
                <ColumnDefinition Width="421*"/>
            </Grid.ColumnDefinitions>
            <TabControl HorizontalAlignment="Left"
                    Height="299"
                    Margin="10,10,0,0"
                    VerticalAlignment="Top"
                    Width="497"
                    SelectionChanged="TabControl_SelectionChanged" Grid.ColumnSpan="2">
                <TabItem Header="Cat">
                    <Grid Background="#FFE5E5E5" x:Name="CatGrid">
                        <GroupBox x:Name="testgrop" Header="Pizza" HorizontalAlignment="Left" Margin="10,46,0,0"
                  VerticalAlignment="Top" Height="175" Width="197" Grid.ColumnSpan="2">
                            <Grid HorizontalAlignment="Left" Height="226" VerticalAlignment="Top"
                  Width="253" Margin="0,0,-2,-12">
                                <Label Content="These are some pizza toppings."
                       HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/>
                                <CheckBox Content="Cheese" HorizontalAlignment="Left"
                          Margin="10,41,0,0" VerticalAlignment="Top"/>
                                <CheckBox Content="Mushrooms" HorizontalAlignment="Left"
                          Margin="10,61,0,0" VerticalAlignment="Top"/>
                                <CheckBox Content="Olives" HorizontalAlignment="Left"
                          Margin="10,81,0,0" VerticalAlignment="Top"/>
                            </Grid>
                        </GroupBox>
                    </Grid>
                </TabItem>
                <TabItem Header="Mouse">
                    <Grid Background="#FFE5E5E5" x:Name="MouseGrid">
                        
                    </Grid>
                </TabItem>
            </TabControl>
        </Grid>


     private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.Source is TabControl) //if this event fired from TabControl then enter
            {
                var item = sender as TabControl;
                TabItem selected = item.SelectedItem as TabItem;
                this.Title = selected.Header.ToString();
                Grid parentPanel = FindParent<Grid>(testgrop);
                Grid ownerselectedgrid = (Grid)selected.Content;

                if (parentPanel == ownerselectedgrid)
                {
                    return;
                }
                if (parentPanel != null && ownerselectedgrid != null)
                {
                    parentPanel.Children.Remove(testgrop);
                    ownerselectedgrid.Children.Add(testgrop);
                }
            }
        }

        public static List<T> FindChildren<T>(DependencyObject parent) where T : DependencyObject
        {
            if (parent == null) return null;

            List<T> children = new List<T>();

            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                T childType = child as T;
                if (childType == null)
                {
                    children.AddRange(FindChildren<T>(child));
                }
                else
                {
                    children.Add((T)child);
                }
            }

            return children;
        }

        public static T FindParent<T>(DependencyObject child) where T : DependencyObject
        {
            //get parent item
            DependencyObject parentObject = VisualTreeHelper.GetParent(child);

            //we've reached the end of the tree
            if (parentObject == null) return null;

            //check if the parent matches the type we're looking for
            T parent = parentObject as T;
            if (parent != null)
                return parent;
            else
                return FindParent<T>(parentObject);
        }







这篇关于C#WPF如何在TabControl.SelectionChanged()上将控件从一个TabItem移动到另一个TabItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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