2009-06-03 156 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属性应该允许您选择一个模板或其他取决于电流模式(查看/编辑)

相关问题