相关文章推荐
任性的棒棒糖  ·  Copy a database - ...·  6 月前    · 
率性的啤酒  ·  亚马逊VaSCL | ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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:

  • Cell border property only affects selected cells. Otherwise i only have the 1px thin lines which can be coloured via VerticalGridLinesBrush
  • If I specifiy a background color on datagrid.cell level it overlays the selection
  • 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>
      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>
        <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".

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.