问题
How might i create a trigger for a Silverlight datagrid in which the cell background color changes based on the cell value? I worked on a WPF project sometime ago and I recall this was quite simple via DataTriggers in the xaml. However this functionality doesn't appear to be available in Silverlight and i'm stuck as to where to start.
Thanks all.
回答1:
Firstly, the replacement for triggers in Silverlight is the VisualStateManager. The VSM is actually much more powerful than triggers as it allows you to execute a StoryBoard when the state changes.
If you don't need animation in your situation, the way I'd solve it would be using an IValueConverter. Create a Border in the DataTemplate, and bind the background brush to your DataItem's property that you want use to change the background brush.
public class BrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
value.ToString() == "Red" ? new SolidColorBrush(Color.Red) : SolidColorBrush(Color.Blue);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedExcpetion();
}
}
Then, your XAML would look something like this:
<Border Background={Binding InterestingProperty,Converter={StaticResource BrushConverter}} />
If you DO need animation, then you're going to want to read up on the VisualStateManager. Essentialy what you'd do is create a Templated or UserControl with a dependency property, then when that property changes determine what state the control should be in, and invoke the visual state manager. The syntax is something like
VisualStateManager.GoToVisualState(yourControlInstance,"TheState",boolUseTransitions);
回答2:
This is a Example to Use true and false Brush
public class BoolToBrushConverter:DependencyObject,IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is bool && (bool)value)
{
return TrueBrush;
}
return FalseBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public Brush FalseBrush
{
get { return (Brush)GetValue(FalseBrushProperty); }
set { SetValue(FalseBrushProperty, value); }
}
// Using a DependencyProperty as the backing store for FalseBrush. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FalseBrushProperty =
DependencyProperty.Register("FalseBrush", typeof(Brush), typeof(BoolToBrushConverter), new PropertyMetadata(null));
public Brush TrueBrush
{
get { return (Brush)GetValue(TrueBrushProperty); }
set { SetValue(TrueBrushProperty, value); }
}
// Using a DependencyProperty as the backing store for TrueBrush. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TrueBrushProperty =
DependencyProperty.Register("TrueBrush", typeof(Brush), typeof(BoolToBrushConverter), new PropertyMetadata(null));}
and in XAML
<UserControl.Resources>
<converter:BoolToBrushConverter x:Key="enabledToBrushConverter"
TrueBrush="White" FalseBrush="Gray" />
</UserControl.Resources>
<TextBlock Foreground="{Binding Element.IsEnabled,
Converter={StaticResource enabledToBrushConverter}, ElementName= your_Element}" />
来源:https://stackoverflow.com/questions/4578970/how-to-create-a-wpf-like-data-trigger-in-silverlight