question

LexiCodes-4589 avatar image
0 Votes"
LexiCodes-4589 asked ·

How to set the Highlight brush to random color

I have this code below that sets the color to random, how to set the selected Listbox item to random??




private void button_Click(object sender, RoutedEventArgs e)
{
Brush brush = new SolidColorBrush(Color.FromRgb((byte)r.Next(1, 255),
(byte)r.Next(1, 255), (byte)r.Next(1, 233)));
label1.Background = brush;
}

windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

Welcome to our Microsoft Q&A platform!

I made a demo,you can try it:

  <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="{Binding Scb}"/>
                                   
                             </Trigger>
                         </ControlTemplate.Triggers>
                     </ControlTemplate>
                 </Setter.Value>
             </Setter>
         </Style>
     </Window.Resources>
     <StackPanel>
         <ListBox SelectionChanged="ListBox1_SelectionChanged" Name="listBox1"  ItemContainerStyle="{DynamicResource _ListBoxItemStyle}" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto">
             <ListBoxItem>1</ListBoxItem>
             <ListBoxItem>2</ListBoxItem>
             <ListBoxItem>3</ListBoxItem>
           
         </ListBox>
     </StackPanel>

   public partial class MainWindow : Window
     {
         public MainWindow()
         {
             InitializeComponent();
             this.DataContext = new MyViewModel();
         }
    
         private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
             this.DataContext = new MyViewModel();
         }
    
     }
    
     public class MyViewModel : INotifyPropertyChanged
     {
         public event PropertyChangedEventHandler PropertyChanged;
    
         Random r = new Random();
         private SolidColorBrush _scb = new SolidColorBrush();
         public SolidColorBrush Scb
         {
             get { return _scb; }
             set { _scb = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Scb")); }
         }
         public MyViewModel()
         {
             Scb= new SolidColorBrush(Color.FromRgb((byte)r.Next(1, 255), (byte)r.Next(1, 255), (byte)r.Next(1, 233)));
    
         }
     }

7253-1.gif

Thanks.



1.gif (37.5 KiB)
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you resolve the issue? If you resolve the issue, please mark the helpful as answer.

0 Votes 0 ·