선택한 ListBox 항목의 배경색 변경
지금까지의 XAML입니다.
<ScrollViewer Grid.Column="1" Grid.RowSpan="2">
<ListBox Background="Black" ItemsSource="{Binding Path=ActiveLog}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Foreground="White">
<TextBlock >Date:</TextBlock>
<TextBlock Text="{Binding Path=LogDate}"/>
</TextBlock>
<TextBlock Grid.Column="1" Grid.Row="0" Foreground="White">
<TextBlock >Severity:</TextBlock>
<TextBlock Text="{Binding Path=Severity}"/>
</TextBlock>
<TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Foreground="LightGray" Text="{Binding Path=Message}"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Template>
<ControlTemplate>
<StackPanel Background="Black" IsItemsHost="True" >
</StackPanel>
</ControlTemplate>
</ListBox.Template>
</ListBox>
</ScrollViewer>
유일한 문제는 선택한 항목의 오른쪽에 파란색 상자가 있다는 것입니다.선택 색상을 변경할 수 있는 방법이 있을 것 같은데 찾을 수 없습니다.
<UserControl.Resources>
<Style x:Key="myLBStyle" TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent"/>
</Style.Resources>
</Style>
</UserControl.Resources>
그리고.
<ListBox ItemsSource="{Binding Path=FirstNames}"
ItemContainerStyle="{StaticResource myLBStyle}">
목록 상자 항목의 스타일을 재정의하기만 하면 됩니다( 참조).TargetType은 ListBoxItem)
또는 HighlightBrushKey를 ListBox에 직접 적용할 수도 있습니다.Setter 속성="백그라운드" 값="투명"은 작동하지 않았습니다.하지만 포그라운드를 블랙으로 설정해야 했어요.
<ListBox ... >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
ListBox를 사용해야 합니다.Item Container Style.
리스트 박스ItemTemplate는 항목의 내용을 표시하는 방법을 지정합니다.그러나 WPF는 여전히 각 항목을 ListBoxItem 컨트롤로 래핑합니다.이 컨트롤은 기본적으로 배경색을 시스템의 하이라이트 색상으로 설정합니다.WPF가 ListBoxItem 컨트롤을 만드는 것을 중지할 수는 없지만, 이 컨트롤을 스타일링하여 배경을 항상 투명 또는 검은색으로 설정하고, 이를 위해 ItemContainerStyle을 사용합니다.
juFo의 답변은 아이템 스타일의 컨텍스트 내에서 시스템 백그라운드 브러시 리소스를 "하이잭"함으로써 구현 가능한 한 가지 방법을 보여줍니다.또 다른, 아마도 더 관용적인 기술은 다음과 같습니다.Setter[Background]속성에 대해 선택합니다.
올바른 스타일링을 위해 HighlightBrushKey와 ControlBrushKey를 모두 설정해야 했습니다.그렇지 않으면 포커스가 있는 동안 투명 HighlightBrusKey가 올바르게 사용됩니다.Bt 컨트롤의 포커스가 흐트러지면(아직 강조 표시됨) ControlBrushKey를 사용합니다.
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</Style.Resources>
를 사용하는 경우.넷 4.5 이상, 사용InactiveSelectionHighlightBrushKey대신ControlBrushKey:
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" />
</Style.Resources>
이게 도움이 됐으면 좋겠네요.
다양한 솔루션을 시도해 봤지만 효과가 있는 솔루션은 없었습니다.조금 더 조사를 한 결과, 여기서 도움이 되는 솔루션을 찾았습니다.
https://gist.github.com/LGM-AdrianHum/c8cb125bc493c1ccac99b4098c7eeb60
<Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="_Border"
Padding="2"
SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="_Border" Property="Background" Value="Yellow"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ListBox ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
Width="200" Height="250"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
<ListBoxItem>Hello</ListBoxItem>
<ListBoxItem>Hi</ListBoxItem>
</ListBox>
이 문제에 대한 첫 번째 구글 결과이기 때문에 도움이 되는 사람도 있을 수 있기 때문에 여기에 올렸습니다.
이렇게 항목 선택을 위해 새 템플릿을 생성해야 합니다.
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border
BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
SnapsToDevicePixels="True">
<ContentPresenter
Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
선택이 중요하지 않은 경우 ScrollViewer로 감싼 ItemsControl을 사용하는 것이 좋습니다.이 조합은 Listbox(실제로 ItemsControl에서 파생된 것)보다 가볍습니다.이 조합은 ItemsControl에서 이미 존재하지 않는 동작을 재정의하기 위해 값싼 해킹을 사용할 필요가 없습니다.
선택 동작이 실제로 중요한 경우, 이것은 분명히 작동하지 않습니다.단, 사용자가 볼 수 없도록 Selected Item Background의 색상을 변경할 경우 혼동만 일으킬 뿐입니다.항목이 선택되었음을 나타내기 위해 다른 특성을 변경하는 경우 이 질문에 대한 다른 답변이 더 적절할 수 있습니다.
다음은 마크업의 개요입니다.
<ScrollViewer>
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
언급URL : https://stackoverflow.com/questions/2138200/change-background-color-for-selected-listbox-item
'source' 카테고리의 다른 글
| Excel VBA - 범위전치 페이스트 복사 (0) | 2023.04.22 |
|---|---|
| XAML에서의 TimeSpan 포맷 방법 (0) | 2023.04.22 |
| ICommand MVVM 구현 (0) | 2023.04.22 |
| SQL - 보유와어디에 (0) | 2023.04.22 |
| Swift에서 이미지(systemName:)에 사용할 수 있는 모든 이미지를 검색합니다.UI (0) | 2023.04.22 |