WPF Grid容器添加删除列

  <Grid >
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0"  Name="grid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Name="stationGridBcolumn0"/>
                <ColumnDefinition Name="stationGridBcolumn1" />
                <ColumnDefinition Name="stationGridBcolumn2" />
                <ColumnDefinition Name="stationGridBcolumn3" />
                <ColumnDefinition Name="stationGridBcolumn4" />
                <ColumnDefinition Name="stationGridBcolumn5" />
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0" Name="g0">
            <Button Name="b0" Content="0"/>
         </Grid>
        <Grid Grid.Column="1" Name="g1">
            <Button Name="b1" Content="1"/>
        </Grid>
        <Grid Grid.Column="2" Name="g2">
            <Button Name="b2" Content="2"/>
        </Grid>
        <Grid Grid.Column="3" Name="g3">
            <Button Name="b3" Content="3"/>
        </Grid>
        <Grid Grid.Column="4" Name="g4">
            <Button Name="b4" Content="4"/>
        </Grid>
        <Grid Grid.Column="5" Name="g5">
            <Button Name="b5" Content="5"/>
        </Grid>
        </Grid>
        <Grid Grid.Row="1" >
            <StackPanel>
            <Button Content="Remove" Click="Button_Click_1"></Button>
            <Button Content="Add" Click="Button_Click_2"></Button>
             <Button Content="Insert" Click="Button_Click_3"></Button>
            </StackPanel>
        </Grid>
    </Grid>
 /// <summary>
        /// 这只是个例子,上一步操作一定是删除操作,Add操作会把删除的列加进来
        /// </summary>
        /// <param name="info"></param>
        public void Add(Info info)
        {
            ColumnDefinition columnDefinition = info.ColumnDefinition;
            UIElement uiElement = info.UIElement;
            Grid.SetColumn(uiElement, grid.Children.Count);
            grid.ColumnDefinitions.Add(columnDefinition);
            grid.Children.Add( uiElement);
        }

        /// <summary>
        /// 这只是个例子,上一步操作一定是删除操作,Insert操作会把删除的列加进来
        /// </summary>
        /// <param name="info"></param>
        /// <param name="colIndex"></param>
        public void Insert(Info info, int colIndex)
        {
            ColumnDefinition columnDefinition = info.ColumnDefinition;
            UIElement uiElement = info.UIElement;
            Grid.SetColumn(uiElement, colIndex);
            grid.ColumnDefinitions.Insert(colIndex, columnDefinition);
            grid.Children.Insert(colIndex, uiElement);
            int index = 0;
            foreach (UIElement child in grid.Children)
            {
                Grid.SetColumn(child, index);
                index++;
            }
        }

        private Info Remove(int colIndex)
        {
            Info info = new Info(grid.ColumnDefinitions[colIndex], grid.Children[colIndex]); 
            grid.ColumnDefinitions.RemoveAt(colIndex);
            grid.Children.RemoveAt(colIndex);
            foreach (UIElement child in grid.Children)
            {
                int curCol = Grid.GetColumn(child);
                if (curCol > colIndex)
                {
                    Grid.SetColumn(child, curCol - 1);
                }
            }
            return info;
        }
        Info info;
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            info = Remove(4);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Add(info);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            Insert(info,4);
        }
相关推荐
勇敢小菜鸟3 小时前
WPF自定义窗口 输入验证不生效
wpf
鲤籽鲲3 小时前
WPF TextBox 输入限制 详解
wpf
鸿喵小仙女4 小时前
C# WPF读写STM32/GD32单片机Flash数据
stm32·单片机·c#·wpf
六点的晨曦4 小时前
WPF的右键菜单项目引入DLL和DllImport特性引入DLL文件的异同点
wpf
一个不正经的林Sir4 小时前
C#WPF基础介绍/第一个WPF程序
开发语言·c#·wpf
可喜~可乐16 小时前
C# WPF开发
microsoft·c#·wpf
界面开发小八哥18 小时前
DevExpress WPF中文教程:Grid - 如何移动和调整列大小?(二)
ui·.net·wpf·界面控件·devexpress·ui开发
界面开发小八哥18 小时前
「实战应用」如何用图表控件SciChart WPF实现应用程序的DPI感知?
信息可视化·wpf·数据可视化·图表·scichart wpf·scichart
明耀2 天前
WPF TabControl 设置item不能点击
wpf
军训猫猫头2 天前
20.抽卡只有金,带保底(WPF) C#
ui·c#·wpf