How to change Style.Triggers in Silverlight
I am new to SilverLight. The xaml below is Style.Triggers
not recognized by Silverlight, I kept commenting it out. What is the alternative for Style.Triggers
in this case? If this VisualStateManager
is how to change it to VisualStateManager
?
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsEnabled" Value="False"/>
<!--<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Control.Background" Value="#d9e2ea"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Control.Background" Value="#9eceec"/>
</Trigger>
</Style.Triggers>-->
</Style>
</ListBox.ItemContainerStyle>
source to share
I think you need to change the background color based on boolean value.
You can create and use "BooleanToBrushConverter"
C # code:
public class BooleanToBrushConverter : IValueConverter
{
public Brush TrueBrush { get; set; }
public Brush FalseBrush { get; set; }
public Brush NullBrush { get; set; }
/// <summary>
/// Converts the data for display.
/// </summary>
public Object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value == null) return NullBrush;
else return (bool)value ? TrueBrush : FalseBrush;
}
/// <summary>
/// Converts the data from display
/// </summary>
public Object ConvertBack(Object value, Type targetType, Object parameter,
System.Globalization.CultureInfo culture)
{
return value == TrueBrush;
}
}
XAML code:
(You need to include this at the top of your XAML) xmlns: lc = "clr-namespace: NamespaceOfYourConverterClass"
(Add below code for converter inside <Grid.Resources> </Grid.Resources> of main <Grid>)
<lc:BooleanToBrushConverter x:Key="myBooleanToColorConverter"
TrueBrush="#9eceec"
FalseBrush="#d9e2ea" />
(Now you need to use this "Converter" when binding the background property of your control)
Like:
<ItemsControl Background="{Binding IsActive, Converter={StaticResource myBooleanToColorConverter}}">
</ItemsControl>
Note. "IsActive" is a boolean property in the ViewModel that returns true (1) or false (0)
source to share