Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf datagrid customization (border, cell corners, etc.)

i'm trying to style a wpf datagrid in xaml to make it look like this image. Is that possible? I tried numerous things, but i still have the following problems:

  1. Cell border property only affects selected cells. Otherwise i only have the 1px thin lines which can be coloured via VerticalGridLinesBrush
  2. If I specifiy a background color on datagrid.cell level it overlays the selection
  3. I have no idea if rounded edges on cell level (also for selections) is even possible

I'm grateful four any help. If it helps i can post a couple of tries here tomorrow.

Edit: This is my code generating the datagrid, as you can see I experimented with background and margin values in datagrid.cellstyle, however it resulted in the above problems:

<DataGrid x:Name="Grid" Height="305" VerticalAlignment="Top" Width="505" BorderThickness="1" 
      AutoGenerateColumns="False" SelectionUnit="Cell" HeadersVisibility="None" ItemsSource="{Binding}" 
      CanUserSortColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserResizeRows="False" 
      IsReadOnly="True" HorizontalAlignment="Left" BorderBrush="White" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" 
      ScrollViewer.CanContentScroll="False" MouseLeftButtonUp="ScreenGrid_MouseLeftButtonUp" Margin="10,10,0,0" Background="#FF000000"
      VerticalGridLinesBrush="White" HorizontalGridLinesBrush="White" SelectedCellsChanged="ScreenGrid_SelectedCellsChanged" >
    <DataGrid.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue"></SolidColorBrush>
        <Style x:Key="DataGridRowStyleColoured" TargetType="{x:Type DataGridRow}">
            <Setter Property="Background" Value="#FF000000" />
        </Style>
    </DataGrid.Resources>
    <DataGrid.RowStyle>
        <StaticResource ResourceKey="DataGridRowStyleColoured"/>
    </DataGrid.RowStyle>
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="BorderThickness" Value="0"/>
            <!--<Setter Property="Margin" Value="5,5,5,5"/> -->
            <!-- <Setter Property="Background" Value="White"/> -->
        </Style>
    </DataGrid.CellStyle>
</DataGrid>
like image 852
spieb Avatar asked Apr 27 '14 15:04

spieb


People also ask

How do I customize the border and gridlines in datagridview?

With the DataGridView control, you can customize the appearance of the control's border and gridlines to improve the user experience. You can modify the gridline color and the control border style in addition to the border styles for the cells within the control.

How to make a rounded corner DataGrid in WPF?

In this article we will see how we can make a rounded corner datagrid in WPF. Fire up Visual Studio 2010 and create a WPF Application, name the solution as RoundedDatagrid. Open the solution in Expression Blend 4. First we need to draw a Border with required value for Corner Radius, in our case we are taking 15.

How to display additional content below the cell values in WPF DataGrid?

The built-in row type includes a drop-down details section that you can use to display additional content below the cell values. Here is a simple style for WPF DataGrid that I want to share with all. <!--Style and template for the resize control on the DataGridColumnHeader.--> <Style x:Key="ColumnHeaderGripperStyle" TargetType=" {x:Type Thumb}">

How to change the background color of a DataGrid cell?

Most of it is done by re-templating the DataGridCell. The inner border creates the rounded corners, while the outer border ensures there is a black background in the "space" around the rounded corners. I've also added a trigger that sets the selected cell's background colour.


1 Answers

This should get you started:-

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
      <Style x:Key="cellStyle" TargetType="DataGridCell">
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="2" />
        <Setter Property="Background" Value="Black" />
        <Setter Property="Template">
          <Setter.Value>
                <ControlTemplate TargetType="DataGridCell">
                    <Border Background="Black" BorderThickness="0">
                      <Border x:Name="border"
                              BorderBrush="White"
                              BorderThickness="2"
                              Background="Black"
                              CornerRadius="5">
                          <ContentPresenter />
                      </Border>
                    </Border>
                    <ControlTemplate.Triggers>
                      <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="true">
                        <Setter TargetName="border" Property="Background" Value="Orange"/>
                      </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>

      <Style x:Key="rowStyle" TargetType="DataGridRow">
        <Setter Property="Padding" Value="0" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Background" Value="Black" />
      </Style>

  <Grid>  
    <DataGrid HeadersVisibility="None" GridLinesVisibility="None" SelectionMode="Single" SelectionUnit="Cell" IsReadOnly="true"
      RowStyle="{StaticResource rowStyle}" CellStyle="{StaticResource cellStyle}" 
      Background="Black" Foreground="White" ItemsSource="{Binding MyData}" />
  </Grid>
</Page>

Most of it is done by re-templating the DataGridCell. The inner border creates the rounded corners, while the outer border ensures there is a black background in the "space" around the rounded corners.

I've also added a trigger that sets the selected cell's background colour. The DataGrid is configured for single-cell selection - it looks like yours will be "Multiple".

like image 133
Andrew Stephens Avatar answered Oct 07 '22 15:10

Andrew Stephens