2009-06-03 63 views
4

如果在窗口左側有一個WPF DataGrid,並且右側區域顯示所選記錄。所選記錄包括Textbox es和ComboBox es,它們在點擊編輯按鈕之前被禁用。所有按預期工作。查看模式和編輯模式之間的WPF DataGrid切換模板

但是,當DataGridSelectedItem被更改時,填充ComboBox似乎有點笨拙。可以使用更輕的控制器,如TextBlock,直到點擊編輯按鈕,然後TextBlock可以切換爲ComboBoxES。

我確定這可以通過某種模板來完成,但是當我嘗試對此進行試驗時,與ComboBoxES關聯的所有事件都報告了錯誤,因爲它們不再存在,因爲它們具有在「查看模式」中用TextBlocks代替。

我可能會討論這個錯誤,所以一些指導將不勝感激。

回答

3

這裏是外觀極好article

申請單點擊編輯所有的細胞在DataGrid

  1. 低於風格粘貼到您的DataGrid
  2. 的資源的方法粘貼到後面
  3. 代碼

申請單點擊編輯只有某些細胞在DataGrid

  1. 的X-:在樣式鍵(前。 )
  2. 風格粘貼到你的DataGrid的資源
  3. 應用風格,你想擁有單一的點擊編輯(如列的CellStyle財產。)
  4. 方法粘貼到代碼背後

    // 
    // SINGLE CLICK EDITING 
    // 
    private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
        DataGridCell cell = sender as DataGridCell; 
        if (cell != null && !cell.IsEditing && !cell.IsReadOnly) 
        { 
         if (!cell.IsFocused) 
         { 
          cell.Focus(); 
         } 
         DataGrid dataGrid = FindVisualParent<DataGrid>(cell); 
         if (dataGrid != null) 
         { 
          if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow) 
          { 
           if (!cell.IsSelected) 
            cell.IsSelected = true; 
          } 
          else 
          { 
           DataGridRow row = FindVisualParent<DataGridRow>(cell); 
           if (row != null && !row.IsSelected) 
           { 
            row.IsSelected = true; 
           } 
          } 
         } 
        } 
    }  
    
    static T FindVisualParent<T>(UIElement element) where T : UIElement 
    { 
        UIElement parent = element; 
        while (parent != null) 
        { 
         T correctlyTyped = parent as T; 
         if (correctlyTyped != null) 
         { 
          return correctlyTyped; 
         } 
    
         parent = VisualTreeHelper.GetParent(parent) as UIElement; 
        } 
        return null; 
    } 
    
+0

感謝Arsenmkrt,我已經看到了一些WPF博士的文章,但你說的沒錯,這個人是一起的就是我要找的線路。 – Mitch 2009-06-20 22:52:35

1

的ContentTemplateSelector屬性應該允許您選擇一個模板或其他取決於電流模式(查看/編輯)

相關問題