【问题标题】:GridSplitter behaviorGridSplitter 行为
【发布时间】:2016-02-04 09:20:17
【问题描述】:

我正在尝试将列和网格拆分器添加到网格中,但无法获得确切的行为。
在用户指定他希望垂直分隔符出现的位置后:

             // Get current Col0 width, size new col and existing Col0
            double col0Width = LayoutRoot.ColumnDefinitions[0].ActualWidth;
            double newCol0Width = col0Width - 5 - pt.X;
            LayoutRoot.ColumnDefinitions[0].Width = new GridLength(newCol0Width);

            // New Column 0
            var c = new ColumnDefinition();
            c.Width = new GridLength(pt.X);
            LayoutRoot.ColumnDefinitions.Insert(0, c);// Attach GridSplitter to left edge of existing first column
            var gss = new GridSplitter();
            gss.Background = new SolidColorBrush(Colors.DarkSlateBlue);
            gss.Width = 5;    gss.Cursor = Cursors.ScrollWE;
            gss.ResizeBehavior = GridResizeBehavior.BasedOnAlignment;
            gss.HorizontalAlignment = HorizontalAlignment.Left;

            LayoutRoot.Children.Add(gss);
            // Add to current left-most colunn
            Grid.SetColumn(gss, 0);

            // Create new column, insert
            // New Column 0
            var c = new ColumnDefinition();
            c.Width = new GridLength(pt.X);
            LayoutRoot.ColumnDefinitions.Insert(0, c);  

            // Move existing content from Col 0 to new Col 1.  

我可以重复此操作并创建任意数量的垂直拆分器。

所需的调整大小行为:移动拆分器仅调整嵌入的列的大小。在分离器的左侧和右侧。
当前的调整大小行为:移动拆分器会将拆分器右侧的所有内容视为一个对象,扩展或缩小拆分器左侧的列,同时将所有内容向右移动。也就是说,如果有 3 列,则移动最左侧的拆分器似乎会将 col 2 推到右侧并缩小 col 3,而不调整 col 2 的大小。
(我希望我解释得足够清楚。)
我尝试将 GridSplitters 放在他们自己的列中,并尝试了各种 GridResizeBehaviors,但没有找到正确的组合。

任何提示将不胜感激......

还有一个相关的问题:在 GridSplitter 的 OnDragDelta 的事件处理程序中,有没有办法阻止拆分器沿某个方向进一步移动?我想防止他们将最右边的列缩小到某个宽度以下,同时允许他们将拆分器移回左侧。
谢谢。

【问题讨论】:

  • 我认为问题在于为列分配宽度。我想创建具有当前列 0 限制的新列。但是,要使网格拆分器按需要工作,似乎我必须计算所有新列宽并将它们设置为适当的 '*'尺寸。

标签: c# wpf gridsplitter


【解决方案1】:

正如我的评论所建议的,列的大小似乎需要为“*”。
所以,在添加了新的列和拆分器之后,我像这样固定了宽度(第一个笨拙的解决方案):

 // Get all col widths, set appropriate '*' sizes.
        foreach (ColumnDefinition col in LayoutRoot.ColumnDefinitions)
        {
            colWidths.Add(col.Width);
            total += col.Width.Value;

            Debug.WriteLine($" Width : {col.Width}");
        }

        Debug.WriteLine($"{total}");

        for (int i = 0; i < LayoutRoot.ColumnDefinitions.Count; i++)
        {
            double d = colWidths[i].Value;
            double ratio = d / total;
            int ii = (int) (ratio * 100);

            LayoutRoot.ColumnDefinitions[i].Width = new GridLength(ii, GridUnitType.Star);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 2011-10-18
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多