2012-07-19 95 views
0

嗨我有一个Datagrid,它绑定到自定义AutoCAD图层对象的ObservableCollection。其中3列是DataGridTextColumns,并且可以正常工作。不过,我也有一个DataGridTemplateColumn,其中包含一个包含标签和Rectangle的StackPanel。我正在使用标签来显示图层的ACI或RGB值,具体取决于它的设置方式以及在矩形中显示的颜色。该矩形有一个鼠标向下的事件,它启动一个颜色选择器对话框,以便用户可以为该图层选择一种新的颜色。此功能起作用。不起作用的是,单元格(标签和矩形)的内容仅显示在选定的行中,并且单元格单击,而它们需要始终可见。DatagridTemplate列内容只有在选定行并单击单元格时才可见

我曾尝试在DataTemplate中使用网格,并使用网格的FocusManager.Focused元素给予矩形焦点,但这并未改变行为。

<t:DataGrid x:Name="layersGrid" ItemsSource="{Binding Layers}" 
    SelectedItem="{Binding SelectedLayer, Mode=TwoWay}" SelectionMode="Single"> 
     <t:DataGridTemplateColumn Visibility="Visible"> 
      <t:DataGridTemplateColumn.CellEditingTemplate> 
       <DataTemplate> 
        <Grid FocusManager.FocusedElement="{Binding ElementName=swatch}"> 
         <StackPanel Orientation="Horizontal"> 
          <Label Content="{Binding Colour.ColourProperty}"/> 
          <Rectangle Name="swatch" Fill="{Binding Colour, Converter={StaticResource colourConverter}}" 
           MouseLeftButtonDown="swatch_MouseLeftButtonDown"/> 
         </StackPanel> 
        </Grid> 
       </DataTemplate> 
      </t:DataGridTemplateColumn.CellEditingTemplate> 
    </t:DataGridTemplateColumn> 
    </t:DataGrid.Columns> 
</t:DataGrid> 

此外,一旦你改变层的颜色在模型视图中,还没有更新的矩形,直到另一行被选中,然后将改变后的一个再次被选择。

private void swatch_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     Colour col = LaunchColourPickerCode(); 
     ((LayersModel)this.Resources[MODEL]).SelectedLayer.Colour = col; 
    } 

回答

0

与他们不显示的问题已得到修复使用CellTemplate而不是CellEditingTemplate

我适应这个页面上surfen的答案来解决选择问题

How to perform Single click checkbox selection in WPF DataGrid?

更换他的方法与此:

private static void GridColumnFastEdit(D ataGridCell cell,RoutedEventArgs e) if(cell == null || cell.IsEditing || cell.IsReadOnly) return;

 DataGrid dataGrid = FindVisualParent<DataGrid>(cell); 
     if (dataGrid == null) 
      return; 

     if (!cell.IsFocused) 
     { 
      cell.Focus(); 
     } 


     DataGridRow row = FindVisualParent<DataGridRow>(cell); 
     if (row != null && !row.IsSelected) 
     { 
      row.IsSelected = true; 
     } 

    } 

并在样本添加事件以获得细胞是在

私人无效swatch_PreviewMouseLeftButtonDown(对象发件人,MouseButtonEventArgs E) {

 DataGridCell cell = null; 

     while (cell == null) 
     { 
      cell = sender as DataGridCell; 
      if (((FrameworkElement)sender).Parent != null) 
       sender = ((FrameworkElement)sender).Parent; 
      else 
       sender = ((FrameworkElement)sender).TemplatedParent; 
     } 


     GridColumnFastEdit(cell, e); 
    } 

同样由于kmatyaszek

相关问题