WPF数据网格自定义(边框、单元格角等)

7
我正在尝试使用XAML样式WPF数据表格,使其看起来像this image。这可能吗? 我尝试了很多方法,但仍然存在以下问题:
  1. 单元格边框属性仅影响选定的单元格。否则,我只有可以通过VerticalGridLinesBrush着色的1px细线
  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>

1
你能否发布一下你的XAML代码,它应该是用来给网格设置样式的? - SnareHanger
1个回答

11

这应该能帮助您开始:

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

大部分工作是通过重新模板化DataGridCell来完成的。内部边框创建了圆角,而外部边框确保在圆角周围的“空间”中有一个黑色背景。

我还添加了一个触发器,用于设置所选单元格的背景颜色。DataGrid已配置为单个单元格选择 - 它看起来像你的将是“Multiple”。


非常感谢,它正好满足了我的需求! - spieb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接