码迷,mamicode.com
首页 > 其他好文 > 详细

WPF多路绑定

时间:2014-06-06 22:36:09      阅读:647      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

WPF多路绑定
多路绑定实现对数据的计算,XAML:
 
引用资源所在位置
bubuko.com,布布扣
xmlns:cmlib="clr-namespace:CommonLib;assembly=CommonLib">
<UserControl.Resources>
        <cmlib:CustomMultiValueConvertor x:Key="MultiValueConverter"/>
</UserControl.Resources>
 
<TextBlock  Foreground="#FF7F7F7F" FontSize="22"  IsHyphenationEnabled="True" >
       <TextBlock.Text>
              <MultiBinding Converter="{StaticResource MultiValueConverter}">
                      <Binding Path="OraElevatorData.shangyuegongzuorigonglvmidu" />
                      <Binding Path="OraElevatorData.benyuegongzuorigonglvmidu" />
               </MultiBinding>
        </TextBlock.Text>
 </TextBlock>    
 
bubuko.com,布布扣

 

用ValueConverter实现计算:
 
bubuko.com,布布扣
public class CustomMultiValueConvertor : IMultiValueConverter
    {       
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if ((double)values[0] == 0)
                {
                    return "--";
                }
                else
                {
                    double bianhua = Math.Abs(((double)values[1] - (double)values[0]) / (double)values[0]);
                    string result = (bianhua * 100).ToString("0") + "%";
                    return result;
                }
            }
            catch(Exception)
            {
                return "--";
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value as string).Split( );
        }
    }
bubuko.com,布布扣

 

WPF多路绑定,布布扣,bubuko.com

WPF多路绑定

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/infly123/p/3765608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!