【解決方法】データグリッド WPF でヘッダーの交差部分の色を変更するにはどうすればよいですか?

プログラミングQA


こんにちは、みんな
よろしければ写真をご覧ください 交差点 — Freeimage.host[^]

ご覧のとおり、DataGrid の RowHeaders と ColumnHeaders の間に空白があります。 それで、色を変更するにはどうすればよいかを知りたいのですが?
ありがとう。

私が試したこと:

RowHeaderとColumnHeaderの背景を変更しようとしましたが、色は変わりませんでした。
インターネットで検索しましたが、コードが見つかりませんでした。

解決策 1

WPF コントロールを理解するには、既定のテンプレートを参照するのが最適です。 DataGrid については、次の場所にあります。 DataGrid のスタイルとテンプレート – WPF .NET Framework | Microsoft Learn[^]。

あなたが指しているのは、 「すべて選択」ボタン の左上隅にある DataGrid

上記のリンクを使用すると、 DataGridSelectAllButtonStyle。 これは、上にリンクされたページにある最初のスタイルです。

XML
<Style TargetType="{x:Type Button}"
       x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, 
       TypeInTargetAssembly={x:Type DataGrid}}">
    <!-- trimmed -->
</Style>

テンプレートを検索すると、それが適用されている場所がわかります。 これは変更する必要があるスタイルです。

XML
<ControlTemplate TargetType="{x:Type DataGrid}">
    <!-- trimmed -->
		<Button Focusable="false"
			Command="{x:Static DataGrid.SelectAllCommand}"
			Style="{DynamicResource {ComponentResourceKey 
			ResourceId=DataGridSelectAllButtonStyle, 
			TypeInTargetAssembly={x:Type DataGrid}}}"
			Visibility="{Binding HeadersVisibility, 
			ConverterParameter={x:Static DataGridHeadersVisibility.All}, 
			Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
			RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
			Width="{Binding CellsPanelHorizontalOffset, 
			RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
    <!-- trimmed -->
</ControlTemplate>

したがって、変更する必要があります DataGridSelectAllButtonStyle そして新しいものを適用します ControlTemplate。 そのページには、作成方法を案内するリンクがあります。 ControlTemplate ページの下部にはサンプルへのリンクがあります。

コメント

タイトルとURLをコピーしました