wpf datagrid自定义(边框,单元格边角等)

spi*_*ieb 5 c# wpf xaml datagrid

我正在尝试在xaml中设置一个wpf数据网格,使其看起来像这个图像.那可能吗?我尝试了很多东西,但我仍然有以下问题:

  1. 单元格边框属性仅影响所选单元格.否则我只有1px细线,可以通过VerticalGridLinesBrush着色
  2. 如果我在datagrid.cell级别指定背景颜色,它会覆盖选择
  3. 我不知道细胞水平上的圆形边缘(也用于选择)是否可能

我很感激四个人的帮助.如果有帮助我明天可以在这里发布几次尝试.

编辑:这是生成数据网格的代码,您可以看到我在datagrid.cellstyle中尝试了背景和边距值,但是它导致了上述问题:

<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>
Run Code Online (Sandbox Code Playgroud)

And*_*ens 7

这应该让你开始: -

<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>
Run Code Online (Sandbox Code Playgroud)

其中大部分是通过重新模板化来完成的DataGridCell.内边框创建圆角,而外边框确保圆角周围的"空间"中有黑色背景.

我还添加了一个触发器来设置所选单元格的背景颜色.DataGrid配置为单细胞选择 - 看起来你的将是"多个".