WPFデータグリッドの列の名前を変更しようとしています。私は列の名前を変更するためにユーザーにコンテキストメニューを提供しています。ユーザーが特定の列の列ヘッダーから名前を変更したら、次のコードとスタイルを使用して列ヘッダーにスタイルを適用します。スタイル内のテキストボックスのINotifyPropertyChanged
private void RenameColumn_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (e != null)
{
if (e.Parameter != null)
{
if ((e.Parameter as DataGridColumnHeader) != null)
{
this.DefaultColHeaderStyle = (e.Parameter as DataGridColumnHeader).Style;
this.RenamedColIndex = (e.Parameter as DataGridColumnHeader).DisplayIndex;
(this.grTestData.ColumnFromDisplayIndex(this.RenamedColIndex)).HeaderStyle = this.grTestData.Resources["RenameColumnHeader"] as Style;
}
}
}
}
私はproperptyに、このテキストボックスをバインドしていますことは:
<Style x:Key="RenameColumnHeader" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid FocusManager.FocusedElement="{Binding ElementName=txtBxRename}">
<TextBox x:Name="txtBxRename" GotFocus="txtBxRename_GotFocus" LostFocus="txtBxRename_LostFocus" KeyDown="txtBxRename_KeyDown" TextChanged="txtBxRename_TextChanged" Text="{Binding Path=NewColName,Mode=TwoWay}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
私は、プロパティNewColNameのためのINotifyPropertyChangedのインタフェースを実装している:
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public string NewColName
{
get
{
return this.newColName;
}
set
{
this.newColName = value;
this.OnPropertyChanged("NewColName");
}
}
それがトリガされていないプロパティ変更私はテキストボックスに入力を開始します。私は、テキストボックスの検証のためにIDataErrorInfoを実装しようとしています。私を案内してください。私のコードに関する他の情報が必要な場合は教えてください。
ヘッダーのコンテキストとは何ですか?あなたは彼に何を提供しますか? –
私は、DataGridをデータテーブルでバインドしており、データグリッドの自動生成カラムを使用しています。 –