Style triggers in Silverlight

I am trying to use style triggers in Silverlight as follows:

   <Path Canvas.Top="20" Stroke="#FF808080" Data="M 0,20 20,0 40,20 Z" StrokeLineJoin="Round">
        <Path.Style>
            <Style TargetType="{x:Type Path}">
                <Setter Property="Fill" Value="DarkGray"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=userControl, Path=PumpRunning}" Value="True">
                        <Setter Property="Fill" Value="DarkGreen"/>        
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Path.Style>
    </Path>

I want to do this so that the path fill value changes if the pump is running or not. The problem is that style triggers are not supported in silverlight!

Anyway, around? Is there a way to do this in code? I studied it, but I'm at a standstill.

thanks

Yang

+5
source share
1 answer

Custom value converter will achieve the same goal.

 public class BoolToBrushConverter : IValueConverter
 {
  public Brush FalseBrush { get; set; }
  public Brush TrueBrush { get; set; }

  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
   if (value == null)
    return FalseBrush;
   else
    return (bool)value ? TrueBrush : FalseBrush;
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
   throw new NotImplementedException("This converter only works for one way binding");
  }
 }

With this converter you can configure your XAML to: -

 <Path Canvas.Top="20" Stroke="#FF808080" Data="M 0,20 20,0 40,20 Z" StrokeLineJoin="Round">
  <Path.Fill>
   <Binding Path="PumpRunning" ElementName="userControl">
    <Binding.Converter>
     <local:BoolToBrushConverter
      FalseBrush="DarkGray" TrueBrush="DarkGreen" />
    </Binding.Converter>
   </Binding>
  </Path.Fill>
 </Path>

, , Path, Path, . , , .

+13

All Articles