如何风格XAML一个DataGrid的左上角? [英] How to style the top-left corner of a DataGrid in XAML?
问题描述
与此相关的问题:数据网格样式表 - 左上角。
我有一个的DataGrid
(尚未完成,原谅样式)。我怎样才能改变使用XAML(如在其他问题而不是C#)?
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)?
下面是我当前的XAML:
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>
奖励:?我怎样才能得到行/列标题2px的边框那里有目前只有1px的边框
Bonus: how can I get a 2px border on the row/column headers where there's currently only a 1px border?
推荐答案
右键因此,如果我们去检查了的默认模板和我们看到的第一个code例子的最顶端;
Right so if we go check out the Default Template and at the very top of the first code example we see;
&LT;! - 风格和模板在DataGrid的左上角。 - &GT按钮;
通过的声明样式模板;
With the declared style template of;
&LT;风格的TargetType ={X:类型按钮}
X:关键={ComponentResourceKey RESOURCEID = DataGridSelectAllButtonStyle,
TypeInTargetAssembly = {X:类型的DataGrid}}&GT;
<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.
至于你的奖金问题,如果我们去看看相同的样式模板的&LT;风格的TargetType ={X:类型DataGridRowHeader}&GT;
我们'会看到硬设置了borderThickness
在 X:名称=rowHeaderBorder
,我们将只改变什么。其中,同样适用于&LT;风格的TargetType ={X:类型DataGridColumnHeader}&GT;
模板,另外还有其它硬盘设置了borderThickness <在
> X:名称=columnHeaderBorder
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"
希望这会有所帮助,干杯!
Hope this helps, cheers!
这篇关于如何风格XAML一个DataGrid的左上角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!