我正在尝试在xaml中设置一个wpf数据网格,使其看起来像this image。那可能吗? 我尝试了很多东西,但我仍然有以下问题:
我很感激四个人的帮助。如果有帮助我明天可以在这里发布几次尝试。
编辑: 这是生成数据网格的代码,您可以看到我在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>
答案 0 :(得分:5)
这应该让你开始: -
<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
来完成的。内边框创造圆角,而外边框确保在&#34;空间中有黑色背景。圆角周围。
我还添加了一个触发器,用于设置所选单元格的背景颜色。 DataGrid配置为单细胞选择 - 看起来你的将是&#34;多个&#34;。