如何以编程方式设置 wpf grid rowspan?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21070755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:29:28  来源:igfitidea点击:

How to set wpf grid rowspan programmatically?

c#.netwpfsilverlightmvvm

提问by user2330678

How to set wpf grid rowspan programmatically? I am using mvvm pattern. I have grid lines enabled and the below doesn't work:

如何以编程方式设置 wpf grid rowspan?我正在使用 mvvm 模式。我启用了网格线,但以下不起作用:

RowDefinition row0 = new RowDefinition();
myGrid.RowDefinitions.Add(row0)

for (int i = 1; i <= RowsCount; i++)
{
    RowDefinition row = new RowDefinition();
    myGrid.RowDefinitions.Add(row);
    TextBlock txt3 = new TextBlock();
    txt3.Text = i.ToString();
    txt3.FontSize = 12;
    txt3.FontWeight = FontWeights.Bold;
    Grid.SetRow(txt3, i);

    myGrid.Children.Add(txt3);
}

ColumnDefinition column0 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(column0);


char c = 'A';
for (int i = 1; i <= ColumnsCount; i++)
{
    ColumnDefinition column = new ColumnDefinition();
    myGrid.ColumnDefinitions.Add(column);                
    TextBlock txt3 = new TextBlock();
    txt3.Text = c.ToString();
    txt3.FontSize = 12;
    txt3.FontWeight = FontWeights.Bold;
    Grid.SetColumn(txt3, i);
    myGrid.Children.Add(txt3);

    switch (i)
    {
        case 1:
            for (int j = 1; j <= RowsCount; j++)
            {
                TextBlock txt = new TextBlock();
                txt.Text = ColumnAROI[j-1].ToString();
                Grid.SetRow(txt, j);
                Grid.SetColumn(txt, i);
                Grid.SetRowSpan(txt, 2);
               // Grid.SetRowSpan(txt, TubeRowSpan[j]);
                myGrid.Children.Add(txt);
            }

            break;
    }

    c++;
}

    for (int j = 1; j <= RowsCount; j++)
        {
            TextBlock txt = new TextBlock();
            txt.Text = ColumnAROI[j-1].ToString();
            Grid.SetRow(txt, j);
            Grid.SetColumn(txt, i);
            Grid.SetRowSpan(txt, TubeRowSpan[j]);
            myGrid.Children.Add(txt);
        }

TubeRowSpan is an ObservableCollection of type int and defined as below. It contains all 1's except at position 2 (3rd element).

TubeRowSpan 是一个 int 类型的 ObservableCollection,定义如下。它包含除位置 2(第三个元素)之外的所有 1。

private ObservableCollection<int> _TubeRowSpan = new ObservableCollection<int>();
    public ObservableCollection<int> TubeRowSpan
    {
        get { return _TubeRowSpan; }
        set
        {
            if (_TubeRowSpan != value)
            {
                _TubeRowSpan = value;
                RaisePropertyChanged(() => TubeRowSpan);
            }
        }
    }

回答by Mark Feldman

You'll have to post the rest of your code, notably how you're creating your row definitions. The following code creates 3 rows for each element and sets the rowspan to 2, it's easy to see from the result that it's working as expected:

您必须发布其余代码,尤其是您如何创建行定义。以下代码为每个元素创建 3 行并将行跨度设置为 2,从结果中很容易看出它按预期工作:

int numChildren = 10;
        int numRows = numChildren * 3;
        for (int j = 0; j < numRows; j++)
            this.myGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(30) });
        for (int j = 0; j < numChildren; j++)
        {
            TextBlock txt = new TextBlock();
            txt.Background = Brushes.AliceBlue;
            txt.Text = "Row " + Convert.ToString(j);
            Grid.SetRow(txt, j*3);
            Grid.SetRowSpan(txt, 2);
            myGrid.Children.Add(txt);
        }

I suspect there's something wrong with your RowDefinitions which is causing them to collapse to a height of 0 making it looklike it's not working when in fact it is. Try replacing your entire code with mine and then replace your own parts back in bit by bit.

我怀疑您的 RowDefinitions 有问题,导致它们折叠到 0 的高度,使其看起来好像不起作用,但实际上是这样。尝试用我的代码替换你的整个代码,然后一点一点地替换你自己的部分。