.NET
.NET ソフトウェア フレームワークに基づく Microsoft テクノロジ。
66 件の質問
このブラウザーはサポートされなくなりました。
Microsoft Edge にアップグレードすると、最新の機能、セキュリティ更新プログラム、およびテクニカル サポートを利用できます。
VS2022, .Net framework4.8.1 です。
プログラムで、ListBoxのSelectedItemの背景に色を付けています。
WPFの仕様なのでしょうが、一方のListBoxで選択したListboxItemの背景色が、他方のListBoxにフォーカスが移ると背景色が薄くなってしまいます。
(図示)始め、左側のListBoxで上のListboxItemを選択して背景色を水色にしたものが、右のListBoxの行をクリックすると背景色が白になってしまいます。
フォーカスを失ったListBoxで、背景色が変更されないようにする方法を教えて下さい。
<xaml>
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp0802"
mc:Ignorable="d"
Title="MainWindow" Height="316" Width="461">
<Grid>
<ListBox x:Name="LB1" HorizontalAlignment="Left" Height="132" Margin="77,107,0,0" VerticalAlignment="Top" Width="148" />
<ListBox x:Name="LB2" HorizontalAlignment="Left" Height="132" Margin="272,107,0,0" VerticalAlignment="Top" Width="148"/>
</Grid>
</Window>
<VB.net>
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim l1 As ListBoxItem
l1 = New ListBoxItem : l1.Content = "1" : Me.LB1.Items.Add(l1)
l1 = New ListBoxItem : l1.Content = "2" : Me.LB1.Items.Add(l1)
l1 = New ListBoxItem : l1.Content = "3" : Me.LB2.Items.Add(l1)
l1 = New ListBoxItem : l1.Content = "4" : Me.LB2.Items.Add(l1)
End Sub
Private Sub LB1_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles LB1.SelectionChanged
For Each k0 As ListBoxItem In Me.LB1.Items
If k0 Is Me.LB1.SelectedItems Then
k0.Background = Brushes.LightSkyBlue
Else
k0.Background = Brushes.White
End If
Next
End Sub
Private Sub LB2_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles LB2.SelectionChanged
For Each k0 As ListBoxItem In Me.LB2.Items
If k0 Is Me.LB2.SelectedItems Then
k0.Background = Brushes.LightSkyBlue
Else
k0.Background = Brushes.White
End If
Next
End Sub
End Class
自己解決しました。
<xaml>
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp0802"
mc:Ignorable="d"
Title="MainWindow" Height="316" Width="461">
<Window.Resources>
<!-- IsSelectとなったListBoxItemの背景色を青でハイライトする。 ここで記述したスタイルを使わないとListBoxからフォーカスが移動すると背景色が白になってしまう -->
<!-- 参考にしたサイト https://stackoverflow.com/questions/57553583/how-to-keep-highlighting-selected-item-of-listbox-even-listbox-lost-focus -->
<Style x:Key="NOuse" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<!-- Revert to the "Windows 7" style template that used "SystemColors.HighlightBrushKey" etc -->
<!-- "ItemBorder" が重要な働きをしている -->
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border
x:Name="ItemBorder"
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<!-- Use the same colours for selected items, whether or not the control has focus -->
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="ItemBorder" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox x:Name="LB1" HorizontalAlignment="Left" Height="132" Margin="117,107,0,0" VerticalAlignment="Top" Width="108"
ItemContainerStyle="{StaticResource NOuse}">
</ListBox>
<ListBox x:Name="LB2" HorizontalAlignment="Left" Height="132" Margin="272,107,0,0" VerticalAlignment="Top" Width="148"
ItemContainerStyle="{StaticResource NOuse}">
</ListBox>
<ListBox x:Name="LB3" Margin="10,107,374,86"
ItemContainerStyle="{StaticResource NOuse}">
</ListBox>
</Grid>
</Window>
<VB.net>
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim l1 As ListBoxItem
l1 = New ListBoxItem : l1.Content = "1" : Me.LB1.Items.Add(l1)
l1 = New ListBoxItem : l1.Content = "2" : Me.LB1.Items.Add(l1)
l1 = New ListBoxItem : l1.Content = "3" : Me.LB2.Items.Add(l1)
l1 = New ListBoxItem : l1.Content = "4" : Me.LB2.Items.Add(l1)
l1 = New ListBoxItem : l1.Content = "5" : Me.LB2.Items.Add(l1)
l1 = New ListBoxItem : l1.Content = "6" : Me.LB2.Items.Add(l1)
End Sub
Private Sub LB1_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles LB1.SelectionChanged
Me.Title = $"{Me.LB1.SelectedIndex}-{Me.LB2.SelectedIndex}"
End Sub
Private Sub LB2_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles LB2.SelectionChanged
Me.Title = $"{Me.LB1.SelectedIndex}-{Me.LB2.SelectedIndex}"
End Sub
End Class