【问题标题】:How to create a WPF-like data trigger in Silverlight?如何在 Silverlight 中创建类似 WPF 的数据触发器?
【发布时间】:2011-06-02 12:19:15
【问题描述】:

如何为 Silverlight 数据网格创建触发器,其中单元格背景颜色根据单元格值发生变化? 前段时间我在一个 WPF 项目上工作,我记得通过 xaml 中的 DataTriggers 这很简单。但是,此功能在 Silverlight 中似乎不可用,我不知道从哪里开始。

谢谢大家。

【问题讨论】:

    标签: silverlight xaml datatrigger


    【解决方案1】:

    首先,Silverlight 中触发器的替代品是 VisualStateManager。 VSM 实际上比触发器更强大,因为它允许您在状态更改时执行 StoryBoard。

    如果您的情况不需要动画,我解决它的方法是使用 IValueConverter。在 DataTemplate 中创建一个边框,并将背景画笔绑定到要用于更改背景画笔的 DataItem 属性。

        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();
        }
    }
    

    然后,您的 XAML 将如下所示:

    <Border Background={Binding InterestingProperty,Converter={StaticResource BrushConverter}} />
    

    如果您确实需要动画,那么您将需要阅读 VisualStateManager。本质上,您要做的是创建具有依赖属性的 Templated 或 UserControl,然后当该属性更改时确定控件应处于什么状态,并调用可视状态管理器。语法类似于

    VisualStateManager.GoToVisualState(yourControlInstance,"TheState",boolUseTransitions);
    

    【讨论】:

    • +1 足够接近。您需要满足值为空的情况。还有它的 GoToState 不是 GoToVisualState。
    【解决方案2】:

    这是一个使用真假画笔的例子

     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));}
    

    在 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}"  />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 2016-11-26
      • 2017-09-26
      相关资源
      最近更新 更多