2
我想要定期动态地使用XAML中的数据绑定定义的网格行和列。使用XAML和Silverlight 4,有没有办法从数据绑定中构建动态GRID行和列?
我知道我可以使用代码背后,但我正在寻找一种方法来纯粹在XAML中。
有什么想法?
我想要定期动态地使用XAML中的数据绑定定义的网格行和列。使用XAML和Silverlight 4,有没有办法从数据绑定中构建动态GRID行和列?
我知道我可以使用代码背后,但我正在寻找一种方法来纯粹在XAML中。
有什么想法?
好了,经过多次绕过球网阅读我编写了以下解决方案:
public class DynamicGrid : Grid
{
public static readonly DependencyProperty NumColumnsProperty =
DependencyProperty.Register ("NumColumns", typeof (int), typeof (DynamicGrid),
new PropertyMetadata ((o, args) => ((DynamicGrid)o).RecreateGridCells()));
public int NumColumns
{
get { return (int)GetValue(NumColumnsProperty); }
set { SetValue (NumColumnsProperty, value); }
}
public static readonly DependencyProperty NumRowsProperty =
DependencyProperty.Register("NumRows", typeof(int), typeof(DynamicGrid),
new PropertyMetadata((o, args) => ((DynamicGrid)o).RecreateGridCells()));
public int NumRows
{
get { return (int)GetValue(NumRowsProperty); }
set { SetValue (NumRowsProperty, value); }
}
private void RecreateGridCells()
{
int numRows = NumRows;
int currentNumRows = RowDefinitions.Count;
while (numRows > currentNumRows)
{
RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
currentNumRows++;
}
while (numRows < currentNumRows)
{
currentNumRows--;
RowDefinitions.RemoveAt(currentNumRows);
}
int numCols = NumColumns;
int currentNumCols = ColumnDefinitions.Count;
while (numCols > currentNumCols)
{
ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
currentNumCols++;
}
while (numCols < currentNumCols)
{
currentNumCols--;
ColumnDefinitions.RemoveAt(currentNumCols);
}
UpdateLayout();
}
}
它的工作原理,但我不知道它是最佳的解决方案。对此有何评论?
也许发布你如何在代码背后做它。我的问题是数据源的样子。它是否具有列名称的数据注释?自动生成列在哪里会失败? – itchi 2010-07-30 16:56:32