强制WPF DataGrid重新生成自己 [英] Force WPF DataGrid to regenerate itself
问题描述
我有一个自定义控件,继承自 DataGrid
,基本上是一个2D DataGrid
(接受 ItemsSource
有两个维度,例如 double [,]
)。
I have a custom control which inherits from DataGrid
and is basically a 2D DataGrid
(accepts an ItemsSource
with two dimensions, such as double[,]
).
我添加了一个特定的 DependencyProperty
,它是 ColumnHeaders
和 RowHeaders
所以我可以定义它们。
I added a specific DependencyProperty
which is ColumnHeaders
and RowHeaders
so I can define them.
这是现在的工作原理:
- 我将2D
ItemsSource
绑定到DataGrid
- 包装方法将将此源转换为经典的
IEnumerable
可绑定到实际的datagrid的ItemsSource
- 自动生成的每行/列都使用事件
AutoGeneratingColumn
&AutoGeneratingRow
为了定义他们的标题
- I bind a 2D
ItemsSource
to theDataGrid
- A wrapper method will take this source to transform it to a classic
IEnumerable
bindable to the actual datagrid'sItemsSource
- Each row / column auto-generated is done using the events
AutoGeneratingColumn
&AutoGeneratingRow
in order to define their header
当我初始化 DataGrid
时,一切正常。
When I initialize the DataGrid
, everything works fine.
之后,我的应用程序的一个用例定义了只有列标题可以改变(通过修改 DependencyProperty
ColumnHeaders
After that, one of the use-cases of my application defines that only the column headers can change (by modifying the DependencyProperty
ColumnHeaders
而且,无论我在这里做什么, DataGrid
不会重新 - 自动生成其列(因此,标题不会以任何方式更改)。
And, whatever I do here, the DataGrid
won't re-autogenerate its columns (and therefore, headers won't be changed in any way).
那么,有没有办法问一下这个 DataGrid
类似嘿,我要你从头重新开始重新生成你的列?因为现在,我无法达到 AutoGeneratingColumn
事件,并调用一个方法,如 InvalidateVisual
将只是重绘网格(而不是重新生成列)。
So, is there a way to ask the DataGrid
something like "Hey, I want you to restart from scratch and regenerate your columns" ? Because for now, I can't reach the AutoGeneratingColumn
event, and calling a method such as InvalidateVisual
will just redraw the grid (and not regenerate columns).
任何想法这里?
我不确定我们需要s ome代码,但...我会放一些所以没有人要求:D
I'm not sure that we need some code but... I'll put some so nobody asks for it :D
/// <summary>
/// IList of String containing column headers
/// </summary>
public static readonly DependencyProperty ColumnHeadersProperty =
DependencyProperty.Register("ColumnHeaders",
typeof(IEnumerable),
typeof(FormattedDataGrid2D),
new PropertyMetadata(HeadersChanged));
/// <summary>
/// Handler called when the binding on ItemsSource2D changed
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private static void ItemsSource2DPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
FormattedDataGrid2D @this = source as FormattedDataGrid2D;
@this.OnItemsSource2DChanged(e.OldValue as IEnumerable, e.NewValue as IEnumerable);
}
// (in the constructor)
AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(DataGrid2D_AutoGeneratingColumn);
void DataGrid2D_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
DataGridTextColumn column = e.Column as DataGridTextColumn;
column.Header = (ColumnHeaders == null) ? columnIndex++ : (ColumnHeaders as IList)[columnIndex++]; //Header will be the defined header OR the column number
column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
Binding binding = column.Binding as Binding;
binding.Path = new PropertyPath(binding.Path.Path + ".Value"); // Workaround to get a good value to display, do not take care of that
}
推荐答案
重置您的ItemsSource,它应该重绘您的DataGrid
Reset your ItemsSource and it should redraw your DataGrid
void ResetDataGrid()
{
var temp = myDataGrid.ItemsSource;
myDataGrid.ItemsSource = null;
myDataGrid.ItemsSource = temp;
}
您也可以刷新绑定,但是我还没有测试看看这是否会重新生成DataGrid:
You might also be able to refresh the binding, but I haven't tested it to see if this will actually regenerate the DataGrid:
void ResetDataGrid()
{
myDataGrid.GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateTarget();
}
这篇关于强制WPF DataGrid重新生成自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!