gpt4 book ai didi

c# - 如何以编程方式设置 wpf 网格 rowspan?

转载 作者:行者123 更新时间:2023-11-30 17:01:26 25 4
gpt4 key购买 nike

如何以编程方式设置 wpf 网格 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 是一个 int 类型的 ObservableCollection,定义如下。它包含除位置 2(第 3 个元素)以外的所有 1。

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

最佳答案

您必须发布其余代码,尤其是如何创建行定义。下面的代码为每个元素创建了 3 行,并将 rowspan 设置为 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);
}

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

关于c# - 如何以编程方式设置 wpf 网格 rowspan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21070755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com