Change the color of text in WPF SelectedItem format and highlight / background Color using C #

I am trying to change the highlighted (selected) color and highlighted color of wpf list text at runtime. I tried to create a style and apply it as follows:

Style s = new Style(typeof(ListBox)); s.Resources.Add(SystemColors.HighlightBrushKey, Setting.ListSelectedColor); s.Resources.Add(SystemColors.HighlightTextBrushKey, Setting.ListSelectedTextColor); lstGames.Style = s; 

But that doesn’t mean anything. Is there any way to achieve this?

EDIT:

In the sentences, I tried to use DynamicResources to achieve this, but so far this has also failed. My code for this:

DYNAMICRESOURCES

 <UserControl.Resources> <Color x:Key="ListTextSelectedColor"/> <Color x:Key="ListSelectedColor"/> </UserControl.Resources> 

Listbox

  <ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Name="lstGames" Margin="20" Grid.Row="2" Grid.Column="2" SelectionChanged="lstGames_SelectionChanged" Grid.RowSpan="2" Grid.ColumnSpan="2" Background="{x:Null}" BorderBrush="{x:Null}" SelectionMode="Single" FontSize="18" FontFamily="OCR A Extended"> <Style TargetType="ListBox"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{DynamicResource ListSelectedColor}"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource ListSelectedColor}"/> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/> </Style.Resources> </Style> </ListBox> 

APPLYING RESOURCES IN C #

 this.Resources["ListSelectedColor"] = SETING.ListSelectedColor.Color; this.Resources["ListTextSelectedColor"] = SETTING.ListSelectedTextColor.Color; 
+5
source share
3 answers

Decision:

 <Window x:Class="ListBoxStyle.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:ListBoxStyle" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <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> </Window.Resources> <Grid> <ListBox ItemContainerStyle="{DynamicResource _ListBoxItemStyle}" Width="200" Height="250" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"> <ListBoxItem>Hello</ListBoxItem> <ListBoxItem>Hi</ListBoxItem> </ListBox> </Grid> </Window> 
+20
source

Thanks to Vinkal and unsuccessful programming, I got everything that works great. I have created the following resources:

 <UserControl.Resources> <SolidColorBrush x:Key="ListTextSelectedColor" x:Shared="False"/> <SolidColorBrush x:Key="ListSelectedColor" x:Shared="False"/> <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="{DynamicResource ResourceKey=ListSelectedColor}"/> <Setter Property="Foreground" Value="{DynamicResource ResourceKey=ListTextSelectedColor}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> 

And then applied the style to my list with:

 ItemContainerStyle="{DynamicResource ResourceKey=_ListBoxItemStyle}" 

And finally, change the solidcolorbrush resources (hence, change the setter values) in my C # code by doing the following:

  this.Resources["ListSelectedColor"] = EmulatorPage.ListSelectedColor; this.Resources["ListTextSelectedColor"] = EmulatorPage.ListSelectedTextColor; 

Thanks to both of you!

+7
source

For all the neighbors there ... do not lose hope! It can be done!

I started with VSS to right-click on the list and use each “Edit Template” and “Edit Additional Templates” for each available item until I find how it works.

You start quite simply with the list box associated with MVVM as usual.

 <ListBox Width="100" x:Name="myComboBox" Margin="8" ItemsSource="{Binding ListBoxListSource}" SelectedIndex="{Binding ListBox}"> </ListBox> 

There are several things that are set in UserControl or Window Resources ....

ListBoxStyle . This stylizes the main container of the list, you can set borders, margins, indents, etc. the main window is here. For my example, I just get rid of everything to undo it.

 <UserControl.Resources> <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Foreground" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Padding" Value="0"/> <Setter Property="Margin" Value="0"/> </Style> </UserControl.Resources> 

ItemContainerStyle . This is the bit that people say cannot be renamed - it contains the "window-selector-blue" panel when selecting an item, but don't be afraid of it. (merging of this section of UserControl.Resources in combination with the previous one).

This section → changing the ItemContainer template from anywhere in Border, setting the top 3 edge for scrolling and customizing the style. Everything we do with this style adds a 3px transparent border to the left and right of the element. Then in Triggers> IsSelected (target myBorder), changing the Brush border to Red.

 <UserControl.Resources> <Style x:Key="ItemContainerStyle" TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border x:Name="myBorder" Padding="0" Margin="0 3 0 0" SnapsToDevicePixels="true" Style="{DynamicResource borderContent}"> <ContentPresenter /> </Border> <ControlTemplate.Resources> <Style x:Key="borderContent" TargetType="Border"> <Setter Property="BorderThickness" Value="3 0 3 0"/> <Setter Property="BorderBrush" Value="Transparent"/> </Style> </ControlTemplate.Resources> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="true"> <Setter TargetName="myBorder" Property="BorderBrush" Value="Red"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> 

ListBoxItemDataTemplate . The next step is to create an element container that displays your data. In my example, YourTextBlockStyler has a trigger on the Text> binding and changes the foreground and background colors of the text. Note that the foreground and background of the Listbox style are set to transparency, so you have to retell them in the TextBlock style if you want to see something.

 <UserControl.Resources> <DataTemplate x:Key="ListBoxItemDataTemplate"> <TextBlock Text="{Binding}" Style="{StaticResource YourTextBlockStyler}"/> </DataTemplate> </UserControl.Resources> 

Back to the list . Now we have set all the styles and templates in the Resources section, where we can update the list with Style = "ItemContainerStyle =" "and ItemTemplate =" "

 <ListBox Width="100" x:Name="myComboBox" Margin="8" ItemsSource="{Binding ListBoxListSource}" SelectedIndex="{Binding ListBox}" Style="{StaticResource ListBoxStyle}" ItemContainerStyle="{StaticResource ItemContainerStyle}" ItemTemplate="{StaticResource ListBoxItemDataTemplate}"> </ListBox> 

Then your raster list is converted magically into a completely redesigned list with a red border selector

Of Drill a standard list box in Completely updated list with red border selector

All without editing one System.ResourceBrush =]

0
source

Source: https://habr.com/ru/post/1214042/


All Articles