Silverlight中的样式触发器 [英] Style triggers in Silverlight

查看:188
本文介绍了Silverlight中的样式触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在silverlight中使用样式触发器,如下所示:

I am trying to use style triggers in silverlight like so:

   <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>

我想这样做,如果泵正在运行,路径的填充值会改变。问题是样式触发器不支持在silverlight!

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

那么还有吗?有没有办法在代码中的方法?

So is there anyway round this? Is there some way of doing this in code? I have looked into it but I am stumped.

感谢

Ian

推荐答案

自定义值转换器将实现类似的目标。

A custom value converter will achieve a similar 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");
  }
 }

有了这个转换器,你可以调整你的XAML : -

With this converter in place you can adjust 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定义,因此acheiving相同的语义。但是,如果您需要使用相同的配对颜色进行大量的这些转换,您可以轻松地将Converter实例放置在页面资源中,并使用正常的速记绑定语法。

Note that since your color choice was local to your Path definition I've embedded an instance of the Converter directly into my Path definition thus acheiving the same semantic. However if you require a number of these conversions using the same pair colors you can just as easily place the Converter instance in a page resource and use the normal shorthand binding syntax.

这篇关于Silverlight中的样式触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆