Hi, in your last posted code you use many instances of MyViewModel and in your Style the reference to Scb is broken. Try following demo. It works without problems:
XAML:
<Window x:Class="WpfApp1.Window24"
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:WpfApp24"
mc:Ignorable="d"
Title="MainWindow" Height="614.262" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<local:MyViewModel x:Key="vm"/>
<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, Source={StaticResource vm}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid DataContext="{StaticResource vm}">
<ListBox SelectionChanged="ListBox1_SelectionChanged" Name="listBox1" ItemContainerStyle="{DynamicResource _ListBoxItemStyle}" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Left" Width="272" Margin="52,119,0,145">
<ListBoxItem >1</ListBoxItem>
<ListBoxItem >2</ListBoxItem>
<ListBoxItem >3</ListBoxItem>
</ListBox>
<StackPanel Margin="0,140,0,-140">
<Label x:Name="label1" MouseDown="label1_MouseDown" Content="" HorizontalAlignment="Left" Height="45" Margin="418,0,0,0" Width="57" Background="Red"/>
<Label x:Name="label2" MouseDown="label2_MouseDown" Content="" HorizontalAlignment="Left" Height="45" Margin="418,0,0,0" Width="57" Background="Green"/>
<Label x:Name="label3" MouseDown="label3_MouseDown" Content="" HorizontalAlignment="Left" Height="45" Margin="418,0,0,0" Width="57" Background="Yellow"/>
<Label x:Name="label4" MouseDown="label4_MouseDown" Content="" HorizontalAlignment="Left" Height="45" Margin="418,0,0,0" Width="57" Background="Blue"/>
<Label x:Name="label5" MouseDown="label5_MouseDown" Content="" HorizontalAlignment="Left" Height="45" Margin="418,0,0,0" Width="57" Background="Magenta"/>
<Label x:Name="label6" MouseDown="label6_MouseDown" Content="" HorizontalAlignment="Left" Height="45" Margin="418,0,0,0" Width="57" Background="Cyan"/>
<Label x:Name="label" MouseDown="label_MouseDown" Content="Random" HorizontalAlignment="Left" Height="45" Margin="418,0,0,0" Width="57"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="61" Margin="56,0,0,0" Width="269" Click="button_Click"/>
</StackPanel>
</Grid>
Code:
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using WpfApp24;
namespace WpfApp1
{
public partial class Window24 : Window
{
public Window24()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e) => vm = this.Resources["vm"] as MyViewModel;
private MyViewModel vm;
public int caseSwitch { get; private set; }
private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) =>
vm.SetScb(caseSwitch);
private void label1_MouseDown(object sender, MouseButtonEventArgs e) => caseSwitch = 1;
private void label_MouseDown(object sender, MouseButtonEventArgs e) => caseSwitch = 7;
private void button_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 101; i++)
{
listBox1.Items.Add("This is item number " + i);
}
}
private void label2_MouseDown(object sender, MouseButtonEventArgs e) => caseSwitch = 2;
private void label3_MouseDown(object sender, MouseButtonEventArgs e) => caseSwitch = 3;
private void label4_MouseDown(object sender, MouseButtonEventArgs e) => caseSwitch = 4;
private void label5_MouseDown(object sender, MouseButtonEventArgs e) => caseSwitch = 5;
private void label6_MouseDown(object sender, MouseButtonEventArgs e) => caseSwitch = 6;
}
}
namespace WpfApp24
{
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 void SetScb(int caseSwitch)
{
switch (caseSwitch)
{
case 7:
Scb = new SolidColorBrush(Color.FromRgb((byte)r.Next(1, 255), (byte)r.Next(1, 255), (byte)r.Next(1, 233)));
break;
case 1:
Scb = new SolidColorBrush(Color.FromRgb(255, 0, 0));
break;
case 2:
Scb = new SolidColorBrush(Color.FromRgb(0, 204, 153));
break;
case 3:
Scb = new SolidColorBrush(Color.FromRgb(255, 255, 51));
break;
case 4:
Scb = new SolidColorBrush(Color.FromRgb(0, 255, 0));
break;
case 5:
Scb = new SolidColorBrush(Color.FromRgb(255, 0, 255));
break;
case 6:
Scb = new SolidColorBrush(Color.FromRgb(0, 255, 255));
break;
default:
Scb = new SolidColorBrush(SystemColors.HighlightBrush.Color);
break;
}
}
}
}