首页 > 代码库 > WPF多路绑定
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(‘ ‘); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。