2015-03-02 71 views
0

如何在DataGridCell.IsEditing爲true時更改DataGridRow顏色?如何在DataGridCell.IsEditing爲true時更改DataGridRow顏色?

當用戶雙擊單元格時,它突出顯示文本並取消選擇整行(即:將行顏色更改爲透明)。只應該突出顯示當前選中的內容,以及在編輯後用戶失去焦點或離開該字段時,該行可以再次突出顯示,具體取決於光標所在的行。

<DataGrid.CellStyle> 
       <Style TargetType="DataGridCell"> 
        <Style.Triggers> 
         <Trigger Property="DataGridCell.IsEditing" Value="True"> 
          <Setter Property="BorderThickness" Value="3" />       
          <Setter Property="BorderBrush" Value="DarkBlue" />  

//Change DataGridRow BackGroudCorlor to transparent.       
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </DataGrid.CellStyle> 

回答

1

您可以使用數據網格的BeginningEdit事件和CellEditEnding事件實現相同的功能。請參閱以下代碼。

<StackPanel>    
     <DataGrid BeginningEdit="DataGrid_BeginningEdit" CellEditEnding="DataGrid_CellEditEnding" x:Name="dgr"></DataGrid> 
    </StackPanel> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     ObservableCollection<person> lst = new ObservableCollection<person>(); 
     for (int i = 0; i < 10; i++) 
     { 
      lst.Add(new person() { FirstName = "Test" + i, LastName = "Lst" + i }); 

     } 
     dgr.ItemsSource = lst; 

    } 

    private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e) 
    { 
     e.Row.Resources.Add(SystemColors.HighlightBrushKey, Brushes.Transparent); 
    } 

    private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) 
    { 
     e.Row.Resources.Remove(SystemColors.HighlightBrushKey);    
    } 
}