如何在XAML中创建DataGrid的左上角? [英] How to style the top-left corner of a DataGrid in XAML?

查看:246
本文介绍了如何在XAML中创建DataGrid的左上角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此问题相关:,在我们看到的第一个代码示例的最上面;

 <! -  DataGrid左上角按钮的样式和模板.--> 

使用以下声明的样式模板:

 < Style TargetType ={x:Type Button}
x:Key ={ComponentResourceKey ResourceId = DataGridSelectAllButtonStyle,
TypeInTargetAssembly = {x:Type DataGrid}} >

我们现在知道/在哪里。之后,只需要在实例级别或模板等上编辑所述风格部分,并重写。



至于你的奖金问题,如果我们请在< Style TargetType ={x:Type DataGridRowHeader}> 中查看相同的样式模板,我们将看到hard-set x:Name =rowHeaderBorder中的BorderThickness ,我们将更改为任何内容。其中同样适用于< Style TargetType ={x:Type DataGridColumnHeader}> 模板,因为还有另一个硬集合 BorderThickness x:Name =columnHeaderBorder


Related to this question: Style datagrid table - Top left corner.

I have a DataGrid (not finished yet, excuse the styles). How can I change the background colour of the top-left corner using XAML (as opposed to C# in the other question)?

Here's my current XAML:

<DataGrid x:Name="DataGrid" HorizontalAlignment="Center" VerticalAlignment="Center"
          ColumnWidth="100" ColumnHeaderHeight="50" RowHeight="50" RowHeaderWidth="115" Padding="5"
          BorderBrush="#FF646464" FontSize="18" FontFamily="Segoe UI"
          CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False"
          Focusable="False" IsEnabled="False" IsReadOnly="True">
    <DataGrid.Background>
        <SolidColorBrush Color="#FFFFFFC8"/>
    </DataGrid.Background>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding In}" Header="In"/>
        <DataGridTextColumn Binding="{Binding Out}" Header="Out"/>
        <DataGridTextColumn Binding="{x:Null}" Header="Hours"/>
    </DataGrid.Columns>
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="Background" Value="#FFFFFFC8"/>
            <Setter Property="BorderBrush" Value="DarkSlateGray"/>
            <Setter Property="BorderThickness" Value="1, 2"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="5"/>
        </Style>
    </DataGrid.ColumnHeaderStyle>
    <DataGrid.RowBackground>
        <SolidColorBrush Color="Transparent"/>
    </DataGrid.RowBackground>
    <DataGrid.RowHeaderStyle>
        <Style TargetType="{x:Type DataGridRowHeader}">
            <Setter Property="Background" Value="#FFFFFFC8"/>
            <Setter Property="BorderBrush" Value="DarkSlateGray"/>
            <Setter Property="BorderThickness" Value="2, 1"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="Padding" Value="5"/>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Item.Day}"/>
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>
</DataGrid>

Bonus: how can I get a 2px border on the row/column headers where there's currently only a 1px border?

解决方案

Right so if we go check out the Default Template and at the very top of the first code example we see;

<!--Style and template for the button in the upper left corner of the DataGrid.-->

With the declared style template of:

<Style TargetType="{x:Type Button}"
       x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, 
               TypeInTargetAssembly={x:Type DataGrid}}">

We now know what/where it is. After that it's just a matter of editing said style part to our hearts content and overriding at the instance level or at the template etc.

As for your bonus question, if we go check out the same style templates at the <Style TargetType="{x:Type DataGridRowHeader}"> we'll see hard-set BorderThickness on the x:Name="rowHeaderBorder" that we'll just change to whatever. Wherein the same applies to the <Style TargetType="{x:Type DataGridColumnHeader}"> template as there's also another hard-set BorderThickness of "1" on the x:Name="columnHeaderBorder"

这篇关于如何在XAML中创建DataGrid的左上角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆