から継承しDataGrid
、基本的に2Dであるカスタムコントロールがあります(などの2つの次元を持つをDataGrid
受け入れます)。ItemsSource
double[,]
DependencyProperty
特定のものを追加したColumnHeaders
のでRowHeaders
、それらを定義できます。
現在の動作は次のとおりです。
- 2D
ItemsSource
をにバインドしますDataGrid
- ラッパー メソッドは、このソースを取得し
IEnumerable
て、実際のデータグリッドにバインド可能なクラシックに変換します。ItemsSource
- 自動生成された各行/列は、ヘッダーを定義するためにイベント
AutoGeneratingColumn
&を使用して行われますAutoGeneratingRow
ここでの問題:
を初期化するDataGrid
と、すべて正常に動作します。
その後、私のアプリケーションのユースケースの 1 つは、列ヘッダーのみを変更できることを定義します (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
}