Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# wpf数据网格定制(边框、单元格角等)_C#_Wpf_Xaml_Datagrid - Fatal编程技术网

C# wpf数据网格定制(边框、单元格角等)

C# wpf数据网格定制(边框、单元格角等),c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我试图在xaml中设计wpf数据网格,使其看起来像。可能吗? 我尝试了很多东西,但我仍然有以下问题: <DataGrid x:Name="Grid" Height="305" VerticalAlignment="Top" Width="505" BorderThickness="1" AutoGenerateColumns="False" SelectionUnit="Cell" HeadersVisibility="None" ItemsSource="{Binding}

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

<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>
  • “单元格边框”属性仅影响选定的单元格。否则,我只有1px细线,可以通过垂直网格线B刷着色
  • 如果我在datagrid.cell级别指定背景色,它将覆盖所选内容
  • 我不知道在单元格级别(也用于选择)是否可以使用圆角
  • 我很感激任何帮助。如果有帮助的话,我明天可以在这里发布几次尝试

    编辑: 这是我生成datagrid的代码,正如您所看到的,我在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>
    

    这应该让您开始:-

    <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配置为单单元格选择-看起来您的将是“多个”。

    您可以发布您的xaml来设置网格样式吗?非常感谢,它正是我所需要的!