2013-07-03 41 views
0
public void MouseSingleClickEditable(object sender, MouseButtonEventArgs e) 
{ 

    if ((!datagrid.HasItems) || datagrid.SelectedIndex < 0) return; 

    DataGridColumn clmn = datagrid.CurrentColumn; 
    if (clmn != null) 
    { 
     String columnType = clmn.GetType().Name; 

      switch (columnType) 
      { 
       case "DataGridTemplateColumn": 
       case "DataGridCheckBoxColumn": 
        row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromItem(datagrid.Items[datagrid.SelectedIndex]); 
        if (!row.IsEditing) 
        { 

         datagrid.IsReadOnly = false; 
         datagrid.BeginEdit(); 
        } 

        break; 
       default: 
        break; 
     } 
    } 
} 

我有2個DatePickers,複選框,並在我的WPF數據網格中的組合框,但同時geeting列的類型,我得到它只是作爲DataGridTemplateColumn而不是類型DataGridDatePickerColumn或DataGridComboboxColumn。如何從DataGridTemplateColumn中提取控件的確切類型。無法得到確切的WPF的DataGrid嵌入式控制型從DataGridTemplateColumn

回答

1
public void MouseSingleClickEditable(object sender, MouseButtonEventArgs e) { 
      var datagrid = new DataGrid(); 
      if ((!datagrid.HasItems) || datagrid.SelectedIndex < 0) return; 

      DataGridColumn clmn = datagrid.CurrentColumn; 
      if (clmn != null) { 
       if (clmn is DataGridCheckBoxColumn) { 
        //do something 
       } else if (clmn is DataGridTemplateColumn) { 
        var templateColumn = (DataGridTemplateColumn)clmn; 

        var rootControlOfCellTemplate = templateColumn.CellTemplate.LoadContent(); 
        var rootControlOfCellEditingTemplate = templateColumn.CellEditingTemplate.LoadContent(); 
        // you can now check for types of the template. CellEditingTemplate is for template in edit mode, and CellTemplate for "non-edit" mode 
        // for example 
        if (rootControlOfCellTemplate is Button) { 
         //do something 
        } 
        if (rootControlOfCellEditingTemplate is DatePicker) { 
         //do something 
        } 
       } 
      } 
     } 

請注意我如何檢查類型。通常你應該這樣做,而不是硬編碼的字符串。

+0

感謝您的回覆,但其顯示的警告消息爲「給定的表達式從來沒有提供(System.Windows.Controls.DatePicker)類型」 – neo

+0

對不起,我現在將更改代碼。 – Vale

+0

現在改變了。 – Vale

相關問題