2012-02-20 50 views
0

我想重命名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> 

我已經實現INotifyPropertyChanged接口財產NewColName:

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。請指導我。如果您需要關於我的代碼的其他信息,請告訴我。

+0

什麼是標題上下文?你給他提供什麼? – 2012-02-20 18:27:21

+0

我綁定了數據表中的數據網格,並使用DataGrid的自動生成列。 – 2012-02-20 18:59:14

回答

2

您可能需要將Binding.UpdateSourceTrigger設置爲PropertyChanged,對於TextBox.Text默認爲LostFocus

+0

我也試過。它仍然沒有工作。 – 2012-02-20 18:23:03

+0

@PriyankThakkar:而且沒有[錯誤]結合工程(http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/30/debugging-data-bindings-in-a-wpf-or-silverlight -application.aspx)? – 2012-02-21 02:20:49

+0

是的,我看不到任何明顯的錯誤。但我會再次檢查並通知您。 – 2012-02-21 04:14:53

0

它已經解決了。

每當我們綁定樣式中聲明的控制,我們需要給一個名稱的窗口。

<Window x:Class="DataGridColumnRename.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:cmd="clr-namespace:DataGridColumnRename" 
    Title="MainWindow" Height="350" Width="525" Name="Me"> 

,以及我們需要指定的ElementName屬性,並指定windown名稱樣式中的控制(在這種情況下,它是「我」)給它。

  <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" Text="{Binding ElementName=Me, Path=NewColName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 
          </Grid> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 

然後只觸發INotifyPropertyChanged。 :) :)感謝您的幫助球員。