自定义面板上的WPF DataGrid

时间:2014-06-24 11:32:54

标签: c# wpf mvvm datagrid panel

我创建了一个自定义面板:

public class TowColumnWrapPanel : Panel
{
  //Here have Dependency Properties: RowSpacing, ColumnSpacing, ColumnsCount

    protected override Size ArrangeOverride(Size arrangeSize)
    {
        List<int> shownChildIndexes = new List<int>();
        for (int i = 0; i < VisualChildrenCount; i++)
        {
            if (Children[i].Visibility == System.Windows.Visibility.Visible)
                shownChildIndexes.Add(i);
        }
        if (shownChildIndexes.Count == 1)
        {
           //on my example I use the code below:

            var child = Children[shownChildIndexes[0]];
            var r = new Rect(0, 0, arrangeSize.Width, arrangeSize.Height);
            if (child as FrameworkElement != null)
                (child as FrameworkElement).MaxWidth = arrangeSize.Width; 
            child.Arrange(r);
        }
        else
        {
           //Some functionality here for lot childs!! 
        }
        return base.ArrangeOverride(arrangeSize);
    }
}

上述类的含义是所有子项都具有相同的大小,并且将在一列/两列上拉伸(对用户可选)

一切运作良好!

DataGrid !!!!

除外

当我的自定义面板上有DataGrid时。它看起来像这样:

调整大小之前(没关系): enter image description here

调整大小后:(以某种方式添加无限列)

enter image description here

我不明白为什么会发生,一点帮助会有所帮助!

1 个答案:

答案 0 :(得分:0)

我错过了这个功能:MeasureOverride - 可以在以下链接中看到:

http://www.codeproject.com/Articles/238307/A-Two-Column-Grid-for-WPF

这就是DG中导致问题的原因。