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(' '); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步