데이터 그리드에서 선택한 행의 색상을 설정하는 방법
데이터 그리드에서 선택한 행의 기본 배경색이 너무 어두워서 읽을 수 없습니다.그것을 무시할 수 있는 방법이 있습니까?
시도해봤습니다
<dg:DataGrid.RowStyle>
<Style TargetType="{x:Type dg:DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="Background" Value="Gainsboro" />
</Trigger>
</Style.Triggers>
</Style>
</dg:DataGrid.RowStyle>
하지만 여전히 아무것도...
위의 해결책은 제 경우 각 셀 주위에 파란색 테두리를 두었습니다.
이것이 저에게 효과가 있었던 해결책입니다.이것은 매우 간단합니다. 이것을 당신의 것에 추가하세요.DataGrid에서 변경할 수 있습니다.SolidColorBrush선형 그라데이션과 같은 다른 브러시에 적용됩니다.
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="#FF0000"/>
</DataGrid.Resources>
알겠습니다. 데이터 그리드에 다음을 추가합니다.리소스 섹션:
<DataGrid.Resources>
<Style TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="dg:DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="#CCDAFF" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
@Seb Kade의 답변의 확장으로 다음을 사용하여 선택된 행과 선택되지 않은 행의 색상을 완전히 제어할 수 있습니다.Style:
<Style TargetType="{x:Type DataGridRow}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
물론 원하는 색상을 입력할 수 있습니다.이것.Style다음과 같은 다른 수집 항목에도 작동합니다.ListBoxItems(교체하는 경우)TargetType="{x:Type DataGridRow}"와 함께TargetType="{x:Type ListBoxItem}"예를 들어).
저는 이런 문제가 있어서 머리를 쥐어뜯을 뻔했는데, 인터넷에서 적절한 답을 찾을 수 없었습니다.WPF 데이터 그리드에서 선택한 행의 배경색을 제어하려고 했습니다.그것은 그냥 그것을 할 수 없었습니다.제 경우, 데이터 그리드에도 CellStyle이 있고, CellStyle이 설정한 RowStyle을 재정의했기 때문입니다.흥미롭게도 CellStyle은 배경색을 설정하지도 않았고, 대신 RowBackground 및 AlternateRowBackground 속성에 의해 설정되었습니다.그럼에도 불구하고 선택한 행의 배경색을 설정하려고 해도 전혀 작동하지 않았습니다.
<DataGrid ... >
<DataGrid.RowBackground>
...
</DataGrid.RowBackground>
<DataGrid.AlternatingRowBackground>
...
</DataGrid.AlternatingRowBackground>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Pink"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="{Binding MyProperty}" />
</Style>
</DataGrid.CellStyle>
선택한 행에 대해 원하는 스타일을 행 스타일에서 셀 스타일로 이동했을 때 다음과 같이 작동했습니다.
<DataGrid ... >
<DataGrid.RowBackground>
...
</DataGrid.RowBackground>
<DataGrid.AlternatingRowBackground>
...
</DataGrid.AlternatingRowBackground>
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="{Binding MyProperty}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Pink"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
누군가 같은 문제가 생길 경우를 대비해 이것을 게시하는 것입니다.
기본 IsSelected 트리거는 Background(백그라운드), Forground(포그라운드) 및 BorderBrush(보더 브러시)의 세 가지 속성을 변경합니다.배경뿐만 아니라 테두리도 변경하려면 스타일 트리거에 포함합니다.
<Style TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="dg:DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="#CCDAFF" />
<Setter Property="BorderBrush" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
행 선택 이벤트가 작동하지 않는 일부 이유
- DataGridCell에 대한 스타일이 설정되었습니다.
- 템플릿 열 사용
- 트리거는 데이터 그리드 행에서 설정됩니다.
이것이 저를 도와준 것입니다.데이터 그리드 셀에 대한 스타일 설정
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
그리고 내부에 레이블이 있는 템플릿 열을 사용하고 있었기 때문에 RelativeSource 바인딩을 사용하여 Foreground 속성을 컨테이너 Foreground에 바인딩했습니다.
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding CategoryName,
Mode=TwoWay,
UpdateSourceTrigger=LostFocus}"
Foreground="{Binding Foreground,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorLevel=1,
AncestorType={x:Type DataGridCell}}}"
Width="150"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
ControlBrushKey를 사용해 보았지만 선택하지 않은 행에 사용할 수 없습니다.선택되지 않은 행의 배경은 여전히 흰색입니다.하지만 행 스타일을 변경해야 한다는 것을 알게 되었습니다.
<DataGrid x:Name="pbSelectionDataGrid" Height="201" Margin="10,0"
FontSize="20" SelectionMode="Single" FontWeight="Bold">
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFFDD47C"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FFA6E09C"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Violet"/>
</DataGrid.Resources>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Background" Value="LightBlue" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
저는 이 문제를 만지작거리며 하루의 대부분을 보냈습니다.DataGrid에서 설정한 RowBackground Property는 에서 변경하려는 모든 시도를 무시하고 있었습니다. 삭제하자마자 모든 것이 작동했습니다. (참고로 DataGridTextColumn에 설정된 Forground도 마찬가지입니다.)
언급URL : https://stackoverflow.com/questions/1223280/how-can-i-set-the-color-of-a-selected-row-in-datagrid
'programing' 카테고리의 다른 글
| C#에서 대용량 파일을 바이트 배열로 읽는 가장 좋은 방법은 무엇입니까? (0) | 2023.05.26 |
|---|---|
| Node.js: 줄 바꿈 없이 콘솔로 인쇄하시겠습니까? (0) | 2023.05.26 |
| C++ 문자열 ==과 비교()의 차이점은 무엇입니까? (0) | 2023.05.26 |
| 수집의 목적은 무엇입니까?체인맵? (0) | 2023.05.26 |
| 바이트[] 배열을 C#의 파일에 쓸 수 있습니까? (0) | 2023.05.26 |