【问题标题】:How to style the top-left corner of a DataGrid in XAML?如何在 XAML 中设置 DataGrid 左上角的样式?
【发布时间】:2014-09-28 06:40:30
【问题描述】:

与此问题相关:Style datagrid table - Top left corner

我有一个DataGrid(尚未完成,请原谅样式)。如何使用 XAML 更改左上角的背景颜色(而不是另一个问题中的 C#)?

这是我当前的 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>

奖励:如何在当前只有 1px 边框的行/列标题上获得 2px 边框?

【问题讨论】:

    标签: c# .net wpf xaml datagrid


    【解决方案1】:

    好吧,如果我们去查看Default Template 并在我们看到的第一个代码示例的最顶部;

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

    使用声明的样式模板:

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

    我们现在知道它是什么/在哪里。之后,只需根据我们的内心内容编辑所述样式部分并在实例级别或模板等处覆盖。

    至于你的额外问题,如果我们在&lt;Style TargetType="{x:Type DataGridRowHeader}"&gt; 上查看相同的样式模板,我们会在x:Name="rowHeaderBorder" 上看到硬设置BorderThickness,我们将更改为任何内容。其中同样适用于&lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt; 模板,因为x:Name="columnHeaderBorder" 上还有另一个“1”的硬设置BorderThickness

    【讨论】:

    • 我怎样才能只为这个实例覆盖左上角的样式?我尝试将Style(和一些样式值)添加到&lt;Window.Resources&gt;,但没有任何变化。
    • 你能分享你的尝试吗?否则,当我稍后有空闲时间时,我会加载一个 WPF 项目并自己测试它,但不能保证这将是最快速的响应,忙碌的一周。
    • 嗨,克里斯,不用担心,感谢您的帮助!我将&lt;Style TargetType="{x:Type Button}" ...&gt; 添加到包含DataGridWindow&lt;Window.Resources&gt; 中。我想我的问题是我不知道如何将这种新样式应用于Button(我的DataGrid XAML 中没有Button)。再次感谢。
    • 你把它放在那里并将 x:Key 更改为DataGridSelectAllButtonStyle ?
    • 你可以在这个答案中看到完整的示例代码:stackoverflow.com/a/30099602/579817
    【解决方案2】:

    经过测试,我发现只有一种设置颜色的方法。 使用 DataGrid 的 Backcolor 和 Button 的 Opacity 0 :-)

    <DataGrid Background="Yellow" RowHeaderWidth="100">
       <DataGrid.Resources>
          <Style TargetType="{x:Type Button}" x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}">
             <Setter Property="Opacity" Value="0" />
          </Style>
       </DataGrid.Resources>
    </DataGrid>
    

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多