0

Hi I have a Datagrid that is bound to an ObservableCollection of custom AutoCAD layer objects. 3 of the columns are DataGridTextColumns and work correctly. However I also have a DataGridTemplateColumn that contains a StackPanel containing a label and a Rectangle. I am using the label to display the ACI or RGB value of the layer depending how it is set and displaying the colour in the rectangle. The rectangle has a mouse down event that launches a colour picker dialog so the user can select a new colour for the layer. This functionality works. What doesn't work is that the contents of the cell (the label and rectangle) are only shown in a row that is selected and the cell clicked on whereas they need to be visible at all times.

I have tried using a Grid inside the DataTemplate and using the Grid's FocusManager.Focused element to give the Rectangle Focus but this hasn't changed the behaviour.

<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>

Additionally, once you change the colour of the layer in the model view, the rectangle hasn't updated until another row is selected and then the changed one is selected again.

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

1 Answers1

1

The problem with them not displaying has been fixed by using CellTemplate instead of a CellEditingTemplate

I adapted surfen's answer on this page to solve the selection problem

How to perform Single click checkbox selection in WPF DataGrid?

Replacing his method with this:

private static void GridColumnFastEdit(DataGridCell 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;
        }

    }

and adding an event on the swatch to obtain the cell it is in

private void swatch_PreviewMouseLeftButtonDown(object sender, 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);
    }

Also thanks to kmatyaszek

Community
  • 1
  • 1
DanBrum
  • 419
  • 1
  • 7
  • 18