首页 > 其他 > 详细

电量显示Binding Converter MVVM

时间:2016-09-15 13:36:46      阅读:254      评论:0      收藏:0      [点我收藏+]

用一个ProcessBar显示电量,低于20%时候,ForeGround为红色,否则为绿色,

页面使用了MVVM绑定到了ViewModel, ProcessBar XAML为

<ProgressBar  Maximum="100" Value="{Binding RemainPercent}" 
 Foreground="{Binding RemainPercent, Converter={StaticResource ForgroundConverter}}" ></ProgressBar>

其中  ForgroundConverter为资源的key

xmlns:converter ="clr-namespace:XXX.XXX"

<UserControl.Resources> <converter:PercentForgroundConverter x:Key="ForgroundConverter"/> </UserControl.Resources>
PercentForgroundConverter 为实现了IValueConverter的类,方法如下,
Brushes的命名空间为System.Windows.Media。
public class PercentForgroundConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double percent = (double)value;
            if (percent<=20)
            {
                return Brushes.Red;
            }
            return Brushes.Green;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

 

电量显示Binding Converter MVVM

原文:http://www.cnblogs.com/pangkang/p/5874629.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!