I have a listbox that has 3 items at design time, my logic works, but if I add a button that adds 100 items at run time it does NOT.
I am trying to switch the color of the selection to a different colors with a case statement.
public int caseSwitch { get; private set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = new MyViewModel(caseSwitch);
}
private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.DataContext = new MyViewModel(caseSwitch);
}
private void label1_MouseDown(object sender, MouseButtonEventArgs e)
{
caseSwitch = 1;
}
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(int caseSwitch)
{
switch (caseSwitch)
{
case 1:
Scb = new SolidColorBrush(Color.FromRgb((byte)r.Next(1, 255), (byte)r.Next(1, 255), (byte)r.Next(1, 233)));
//Console.WriteLine("Case 1");
break;
case 2:
Scb = new SolidColorBrush(Color.FromRgb(255, 160, 122));
break;
default:
Scb = new SolidColorBrush(SystemColors.HighlightBrush.Color);
break;
}
}