zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Session服务器配置指南与使用经验
    string和byte[]的转换 (C#)
    错误1已授予对“SqlAccess...的友元访问解决方法
    网络视频会议 二
    Editplus配置环境变量
    TSC 条码打印机 Dll 说明
    源码C#事例网址
    C#中的日志类
    分页事例 比较好的
    dotnet 网络编程 tcp
  • 原文地址:https://www.cnblogs.com/zhangtao/p/2347501.html
Copyright © 2011-2022 走看看