我想在运行时创建一个网格。这是用于说明我正在寻找的xaml代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
Name='Test'>
<Grid.RowDefinitions>
<RowDefinition Height='*' />
<RowDefinition Height='*' />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width='*' />
<ColumnDefinition Width='*' />
<ColumnDefinition Width='*' />
<ColumnDefinition Width='*' />
<ColumnDefinition Width='*' />
</Grid.ColumnDefinitions>
</Grid>
上面的xaml代码很好,除了它是静态的和固定的。我希望应用程序在运行时根据用户输入创建列和行定义。所以,我创建了一个名为initiate grid的方法:
private void initiateGrid()
{
int numberOfColumn = 10;
for (int j = 0; j < (numberOfColumn / 5) + 1; j++)
{
RowDefinition r1 = new RowDefinition();
Test.RowDefinitions.Add(r1);
for (int i = 0; i < numberOfColumn; i++)
{
ColumnDefinition c1 = new ColumnDefinition();
c1.Width = new GridLength(1, GridUnitType.Star);
Test.ColumnDefinitions.Add(c1);
TextBlock tb = new TextBlock();
tb.FontSize = 20;
tb.VerticalAlignment = VerticalAlignment.Top;
tb.HorizontalAlignment = HorizontalAlignment.Stretch;
tb.Text = string.Format("Text row {0}, column {1}", j, i);
Test.Children.Add(tb);
Grid.SetColumn(Test, i);
Grid.SetRow(Test, j);
}
}
}
我在MainPage中有initializeComponent后调用它。
public MainPage()
{
this.InitializeComponent();
initiateGrid();
}
我没有收到编译器错误或运行时错误,但我没有看到我试图填充的文本块来验证位置的准确性。 有人可以为我揭开光明吗?我的代码出了什么问题?
谢谢!
答案 0 :(得分:1)
这正是我的代码中出错的地方:
1)我创建了太多列
2)我应该将行和列分配给文本块,而不是网格 这就是正确的代码:
<div id="chartContainer">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://dimplejs.org/dist/dimple.v1.1.1.min.js"></script>
</div>
<script>
console.log(data);
var svg = dimple.newSvg("#chartContainer", 1000, 1000);
var data = [
{ '"loc"': '"Hamburg, Deutschland"', '"title"': "CNC-Dreher/in" },
{ '"loc"': 'M?nchen (Kreis), Bayern', '"title"': "Data Scientist" },
{ '"loc"': '"D?sseldorf, Nordrhein-Westfalen"', '"title"': "Praktikant m|w" },
{ '"loc"': '"Rheinland-Pfalz, Deutschland"', '"title"': "Senior Softwareentwickler Logistikdienstleistung(w/m)" },
{ '"loc"': '"D?sseldorf, Nordrhein-Westfalen"', '"title"': "Projektmanager (m/w) Information Engineering" },
{ '"loc"': '"Grimma, Leipzig (Kreis)"', '"title"': "Softwareentwickler (m/w)" },
{ '"loc"': '"Hannover, Region Hannover (Kreis)"', '"title"': "Bauleiter (m/w)" },
{ '"loc"': '"M?nchen (Kreis), Bayern"', '"title"': "Backend Software Developer (m/f)" },
{ '"loc"': '"Ratingen, Mettmann (Kreis)"', '"title"': "Ausbildung Industriekaufmann (m/w)" },
{ '"loc"': '"Bayern, Deutschland"', '"title"': "Dualer Studiengang Bachelor of Arts (Fachrichtung Bank/Vertrieb) Bayern" },
{ '"loc"': '"Landshut, Landshut (Kreis)"', '"title"': "Spezialist CFK m/w" },
{ '"loc"': '"Frankfurt am Main, Hessen"', '"title"': "Data Analyst / Consultant analytisches CRM (m/w) f?r unseren Premium Partner EDEKA" },
{ '"loc"': '"Hamburg, Deutschland"', '"title"': "Produktmanager (m/w) im Bereich Privat Kraft" },
{ '"loc"': '"Unterf?hring, M?nchen (Kreis)"', '"title"': "Controller / Assistent der Gesch?ftsleitung" },
{ '"loc"': '"Berlin, Deutschland"', '"title"': "Projektmanager (m/w)" },
{ '"loc"': 'Royal Leamington Spa', '"title"': "SOFTWARE TEST ENGINEER / QA (m/w)" },
{ '"loc"': '"Lauf an der Pegnitz, N?rnberger Land (Kreis)"', '"title"': "Bilanzbuchhalter/in" },
{ '"loc"': '"Buchenb?hl, N?rnberg"', '"title"': "Inhouse Business Intelligence Berater (m/w)" },
{ '"loc"': '"Berlin, Deutschland"', '"title"': "Praktikant Vertriebscontrolling (m/w) f?r die Dauer von 6 Monaten (Pflichtpraktikum)" }
];
var chart = new dimple.chart(svg,data);
chart.setBounds(100, 100, 500, 300);
// Add a Brand ordinal axis on "x"
var x = chart.axes.push(new dimple.axis(chart, "x", '"loc"', null));
// Add a Sales Volume linear numerical axis on "y"
var y = chart.axes.push(new dimple.axis(chart, "y", null, '"title"'));
// // Add a time axis on "x" for the date field
var lines = chart.addSeries(["project"], dimple.bar, [x, y]);
lines.data = data;
lines.lineWeight = 5;
lines.lineMarkers = true;
chart.draw();
</script>
答案 1 :(得分:0)
这是您尝试使用AttachedProperty https://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/
实现的目标的一个很好的示例在您的XAML中,您只需使用......
XAML
<Grid local:GridHelpers.RowCount="{Binding RowCount}"
local:GridHelpers.ColumnCount="{Binding ColumnCount}" />
视图模型
public MainWindowVM()
{
this.RowCount = 4;
this.ColumnCount = 5;
}
private int _RowCount;
public int RowCount
{
get { return _RowCount; }
set { _RowCount = value; NotifyPropertyChanged("RowCount"); }
}
private int _ColumnCount;
public int ColumnCount
{
get { return _ColumnCount; }
set { _ColumnCount = value; NotifyPropertyChanged("ColumnCount"); }
}