2011-03-03 65 views
5

我有一個DataGridTemplateColumn。在其CellEditingTemplate裏面,我放了一個DatePicker控件。現在,如果我想編輯日期,我必須點擊三次才能開始編輯DatePicker。只有兩次點擊,有人能讓我知道如何將DatePicker置入編輯模式嗎?此外,如果DataGridTemplateColumn得到關注,則鍵盤輸入不會將DatePicker也置於編輯模式。如果它可以被修復,它會很好。帶DatePicker的DataGridTemplateColumn需要三次點擊來編輯日期

+0

您可以顯示XAML?這將幫助我們重現並幫助您解決問題。 – keyle 2011-03-03 04:20:06

回答

8

你必須重寫PrepareCellForEditDataGridTemplateColumn如下:

public class DataGridDateColumn:DataGridTemplateColumn 
{ 
    protected override object PrepareCellForEdit(FrameworkElement editingElement, 
               RoutedEventArgs editingEventArgs) 
    { 
     editingElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.First)); 
     return base.PrepareCellForEdit(editingElement, editingEventArgs); 
    } 
} 

XAML

<Custom:DataGrid x:Name="dgData" SelectionUnit="Cell" AutoGenerateColumns="False" CanUserAddRows="False"> 
    <Custom:DataGrid.Columns> 
     <Custom:DataGridTextColumn Binding="{Binding Subject}" Header="Subject" Width="*"/> 
     <Custom:DataGridTextColumn Binding="{Binding RaisedBy}" Header="Raised By" Width="100"/> 

     <DatePickerDGWPF:DataGridDateColumn Header="Raised On" Width="250"> 
      <DatePickerDGWPF:DataGridDateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding RaisedOn}" /> 
       </DataTemplate> 
      </DatePickerDGWPF:DataGridDateColumn.CellTemplate> 
      <DatePickerDGWPF:DataGridDateColumn.CellEditingTemplate> 
       <DataTemplate> 
        <Custom:DatePicker SelectedDate="{Binding RaisedOn}"/>  
       </DataTemplate> 
      </DatePickerDGWPF:DataGridDateColumn.CellEditingTemplate> 
     </DatePickerDGWPF:DataGridDateColumn> 
    </Custom:DataGrid.Columns> 
</Custom:DataGrid> 
相關問題