2013-12-11 30 views
0

我有一個DataGridTextColumn,但是當我點擊進入單元格時,文本現在變得可編輯了,但是當我雙擊文本時它不會選擇所有文本(或者只是當前單詞)。DataGridTextColumn雙擊全選

    <DataGridTextColumn ClipboardContentBinding="{Binding Path=Name}" SortMemberPath="Name" 
             Header="Name" 
             Binding="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=Explicit}" CanUserReorder="True" CanUserSort="True" CanUserResize="True" Width="SizeToHeader" /> 

回答

0

其中一個解決方案是爲datagirdcell設置樣式,爲每個單元設置MouseDoubleClick事件。

<Window.Resources> 
    <Style TargetType="DataGridCell"> 
     <EventSetter Event="MouseDoubleClick" Handler="CellDoubleClick"/> 
    </Style> 
</Window.Resources> 

和代碼背後...

/// <summary> 
    /// Select all text in DataGridCell on DoubleClick 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void CellDoubleClick(object sender, RoutedEventArgs e) 
    { 
     DataGridCell cell = null; 
     TextBox textBox = null; 

     cell = sender as DataGridCell; 
     if (cell == null) 
     { 
      return; 
     } 

     textBox = cell.Content as TextBox; 
     if (textBox == null) 
     { 
      return; 
     } 
     textBox.SelectAll(); 
    }