【发布时间】: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