Configuring wpf datagrid (border, cell angles, etc.)

I am trying to create a wpf datagrid in xaml so that it looks like this image . Is it possible? I have tried many things, but I still have the following problems:

  • The cell border property affects only selected cells. Otherwise, I only have 1px thin lines that can be colored through VerticalGridLinesBrush
  • If I set the background color at the datagrid.cell level, it imposes a selection
  • I don't know if rounding of borders at the cell level is possible (also for choice)

I am grateful to four for their help. If this helps, I can post a couple of attempts here tomorrow.

Edit: This is my datagrid generating code, as you can see that I experimented with the background and field values ​​in the datagrid.cellstyle file, however this led to 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>
+4
source share
1 answer

This should help you: -

<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 them are done by changing the template DataGridCell. The inner border creates rounded corners, and the outer border provides a black background in the "space" around the rounded corners.

I also added a trigger that sets the selected background color of the cell. The DataGrid is configured to select a single cell - it looks like yours will be "Multiple".

+4
source

Source: https://habr.com/ru/post/1538286/


All Articles