WPF多路绑定

WPF多路绑定
多路绑定实现对数据的计算,XAML:
 
引用资源所在位置
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>    
 

 

用ValueConverter实现计算:
 
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(' ');
        }
    }

 

posted @ 2014-06-03 13:54  Infly  阅读(1830)  评论(0编辑  收藏  举报