DataGrid
を継承するカスタムコントロールがあり、基本的には2D DataGrid
です(double[,]
のように2次元のItemsSource
を受け入れます)。WPF DataGridを強制的に再生成する
DependencyProperty
を追加しました。これはColumnHeaders
とRowHeaders
です。これらを定義することができます。ここで
は、それが今どのように動作するかです:
- 私はラッパー・メソッドは、実際のデータグリッドの
ItemsSource
に古典
- に2D
ItemsSource
をバインド - 自動生成された各行/列は、ヘッダを定義するために
AutoGeneratingColumn
&AutoGeneratingRow
というイベントを使用して実行されます。
IEnumerable
バインド可能にそれを変換するために、このソースがかかります
DataGrid
ここで問題:
私はDataGrid
を初期化すると、すべてが正常に動作します。自分のアプリケーションのユースケースの一つは、私はここで行うものは何でも、DependencyProperty
ColumnHeaders
を修正することによって(のみ列ヘッダーが変更できることを定義した後
、DataGrid
は、その再自動生成しません
したがって、DataGrid
には「ねえ、最初から再起動して列を再生成してもらいたいですか」と尋ねる方法はありますか?現在、AutoGeneratingColumn
イベントには到達できません。InvalidateVisual
などのメソッドを呼び出すと、グリッドが再描画されます列を再生成しません)。
ここに任意のアイデアはありますか?
私たちはいくつかのコードが必要であることはよく分からないけど...私はいくつかを入れますので、誰もがそれを要求していない: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プロパティをリセットしてみません –
それは実際に(と私は-__-それ以前については考えていないと信じてすることはできません)動作しますが、それを行うには任意の「賢く」方法は何ですか? – Damascus
@Damascus itemsourceの設定を解除して設定するよりスマートな方法を見つけましたか?私もそれを探しています。 – Haozhun