There are a lot of people providing coloring answers with meta-data programming which works fine, for simple color changes. But, most people really have a need to color based on data values in the model. Can someone provide a c# example code fragment that does the same thing as
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" color="Red" />
and could be called in this context
public class MyItem : ListBoxItem { public override void OnSelected( RoutedEventArgs e ) { base.OnSelected( e ); Foreground = someBoolean ? Brushes.White: Brushes.Black; Code_To_Coerce_HighlightBrushKey_To_The_Correct_Color(Brushes.Red); } public override void OnUnSelected( RoutedEventArgs e ) { base.OnUnSelected( e ); Foreground = someBoolean ? Brushes.Red : Brushes.Black; Code_To_Coerce_HighlightBrushKey_To_The_Correct_Color(SystemColors.WindowBrush); } }
This would be the most helpful. Meta-data, XAML programming, of course, lets Microsoft recolor or "change" most things about your coloring with themes and OS platform changes. But that doesn't work for apps which need to color each ListBoxItem differently, including selection background and foreground changes.
UPDATE:
It is possible to use the follow code
public class SiteDataItem : ListBoxItem {
SiteInfo site;
protected override void OnUnselected( RoutedEventArgs e ) {
base.OnUnselected(e);
Foreground = site.Enabled ? Brushes.Black : Brushes.Red;
Style s = new Style(typeof(ListBox));
theSiteList.Style = s;
}
protected override void OnSelected( RoutedEventArgs e ) {
base.OnSelected(e);
Foreground = site.Enabled ? Brushes.White : Brushes.White;
if( site.Enabled == false ) {
Style s = new Style(typeof(ListBox));
s.Resources.Add(SystemColors.HighlightBrushKey, Brushes.Red);
theSiteList.Style = s;
}
}
}
to effect the style changes, on selection and deselection, programmaticly and get the desired behavior. What I'm trying to understand now, is how to tie into XAML's triggers and properties to drive this kind of behavior from just having data in a listbox's model.