将项目从一个网格移动到另一个网格

时间:2012-04-03 06:58:41

标签: c# wpf xaml

好的,这是我的XAML:

<Window x:Class="nathan___visual_studio_panes___layers.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">

    <DockPanel>

        <StackPanel DockPanel.Dock="Top">

            <Button Name="move_ellipse2_to_GridA" Click="move_ellipse2_to_GridA_Click">
                Move ellipse2 to GridA
            </Button>

            <Button Name="move_ellipse3_to_GridA" Click="move_ellipse3_to_GridA_Click">
                Move ellipse3 to GridA
            </Button>

        </StackPanel>

        <Grid Grid.IsSharedSizeScope="True" Background="AliceBlue">

            <Grid Name="gridA">                
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <Ellipse Grid.Column="0" Grid.Row="0" Fill="CornflowerBlue"/>                
            </Grid>

            <Grid Name="gridB" Visibility="Visible">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <Ellipse Grid.Column="1" Name="ellipse2" Fill="Aquamarine"/>

                <GridSplitter Name="gridB_grid_splitter" Width="5" Grid.Column="1" HorizontalAlignment="Left"/>

            </Grid>

            <Grid Name="gridC" Visibility="Visible">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <Ellipse Grid.Column="1" Name="ellipse3" Fill="Cornsilk"/>

                <GridSplitter Name="gridC_grid_splitter" Width="5" Grid.Column="1" HorizontalAlignment="Left"/>
            </Grid>

        </Grid>

    </DockPanel>

</Window>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace nathan___visual_studio_panes___layers
{  
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ColumnDefinition GridA_column_for_ellipse2 = new ColumnDefinition();
        ColumnDefinition GridA_column_for_ellipse3 = new ColumnDefinition();


        public MainWindow()
        {
            InitializeComponent();
        }

        private void move_ellipse2_to_GridA_Click(object sender, RoutedEventArgs e)
        {
            gridB.Children.Remove(gridB_grid_splitter);

            var i = gridA.ColumnDefinitions.Count;

            gridA.ColumnDefinitions.Add(GridA_column_for_ellipse2);

            gridB.Children.Remove(ellipse2);
            gridA.Children.Add(ellipse2);
            Grid.SetColumn(ellipse2, i);

            var grid_splitter = new GridSplitter() { Width = 5, HorizontalAlignment = HorizontalAlignment.Left };
            gridA.Children.Add(grid_splitter);
            Grid.SetColumn(grid_splitter, i);

            Console.WriteLine(i);
        }

        private void move_ellipse3_to_GridA_Click(object sender, RoutedEventArgs e)
        {
            gridC.Children.Remove(gridC_grid_splitter);

            var i = gridA.ColumnDefinitions.Count;

            gridA.ColumnDefinitions.Add(GridA_column_for_ellipse3);

            gridC.Children.Remove(ellipse3);
            gridA.Children.Add(ellipse3);
            Grid.SetColumn(ellipse3, i);

            var grid_splitter = new GridSplitter() { Width = 5, HorizontalAlignment = HorizontalAlignment.Left };
            gridA.Children.Add(grid_splitter);
            Grid.SetColumn(grid_splitter, i);

            Console.WriteLine(i);
        }         
    }
}

如果我启动程序并单击“将ellipse2移动到GridA”和“将ellipse3移动到GridA”按钮,我会得到“正确的东西”。即三个椭圆和两个网格分割器都出现在窗口中。

但是,如果我启动程序,拖动网格分割器(移动ellipse3),单击“将ellipse2移动到GridA”,拖动网格分割器为ellipse2(绿色),最后单击“将ellipse3移动到GridA” ,ellipse3完全消失了!

我在这里做错了什么? :-) I.e。按下两个按钮应该会导致所有三个椭圆显示在窗口中,无论是否有任何分割器拖动。

我知道这是一个奇怪的演示程序。 Adam Nathan的WPF Unleashed中的一个示例是Visual Studio开始屏幕的模型。上面的演示程序只是探索了一种不同的对接/取消对接窗格的技术。

感谢任何提示或提示。我是WPF新手。 : - )

1 个答案:

答案 0 :(得分:2)

解决方案

将以下代码放在move_ellipse3_to_GridA_Click方法

的末尾
foreach (ColumnDefinition column in gridA.ColumnDefinitions)
    column.Width = new GridLength(1, GridUnitType.Star);

解释

问题在于GridSplitter修改了ColumnDefinition.Width。它保留了明星,但它变成了类似“357 *”的东西。这就是为什么gridA中的最后一列变得非常小。如果您在move_ellipse3_to_GridA_Click处理程序的开头放置断点,则可以看到它,重现您的“错误”场景并再次按下该按钮。检查gridA.ColumnDefinitions [0]和[1]宽度。

<强>意识

只是为了让世界变得更美好:建议将GridSplitter置于专用的ColumnDefinitionRowDefinition)中,WidthHeight)设置为Auto以及GridSplitterCenterStretch的对齐。

如果只有一个RowDefinitionColumnDefinition并且不需要将元素的RowColumn设置为0,则无需定义{{1}}或{{1}}默认值。