• Silverlight数据绑定转换示例


     <Grid x:Name="LayoutRoot" Background="White">
    <Grid.Resources>
    <local:ColorConverter x:Key="ColorConverter"></local:ColorConverter>
    </Grid.Resources>

    <Ellipse Width="300" Height="200" Fill="{Binding Status,Converter={StaticResource ColorConverter},Mode=TwoWay}"></Ellipse>

    </Grid>
    public enum TrafficStatus
    {
    Stop,Ready,Go
    }

    public class TrafficLight : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;
    public TrafficStatus Status
    {
    get { return status; }
    set
    {
    status = value;
    if (PropertyChanged != null)
    {
    PropertyChanged(this, new PropertyChangedEventArgs("status"));
    }
    }
    }

    private TrafficStatus status;
    }

    public class ColorConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
    TrafficStatus status = (TrafficStatus)value;
    SolidColorBrush brush = new SolidColorBrush(Colors.Red);
    switch (status)
    {
    case TrafficStatus.Stop:
    break;
    case TrafficStatus.Ready:
    brush = new SolidColorBrush(Colors.Orange);
    break;
    case TrafficStatus.Go:
    brush = new SolidColorBrush(Colors.Green);
    break;
    default:
    break;
    }
    return brush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
    return null;
    }
    }


            private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
    TrafficLight traffic = new TrafficLight()
    {
    Status = TrafficStatus.Stop
    };
    this.DataContext = traffic;
    }

    Silverlight数据绑定转换.rar

    作者:wpf之家
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    ASP.NET Core
    ASP.NET Core
    ASP.NET Core
    ASP.NET Core
    通用查询设计思想(2)- 基于ADO.Net的设计
    API接口通讯参数规范(2)
    Centos7 安装selenium(python3.7 centos7 )
    mysql (create temporary table table_name )临时表创建
    C# 字符串 String、StringBuffer和StringBuilder的区别
    安装Elasticsearch
  • 原文地址:https://www.cnblogs.com/wpf123/p/2347267.html
Copyright © 2020-2023  润新知