WPF 多值转换器
和普通转换器原理差不多
先看效果 最后一行的值是前面的总和
首先创建一个类继承接口 IMultiValueConverter
class NumMultiConvert : IMultiValueConverter { #region Field 字段 #endregion #region Constructor 构造函数 #endregion #region Property 属性 #endregion #region Event 事件 #endregion #region Method 方法 #endregion public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return values.Where(i => i != null).Select(i => System.Convert.ToDouble(i)).Sum().ToString(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
前台引用转换器
使用
"Vertical"> "tb1" Width="100" FontSize="30" Text="25" /> "tb2" Width="100" FontSize="30" Text="75" /> "tb3" Width="100" FontSize="30" Text="55" /> "tb4" Width="100" FontSize="30" Text="10" /> "100" FontSize="30"> "{StaticResource NumMultiConvert}"> "tb1" Path="Text" /> "tb2" Path="Text" /> "tb3" Path="Text" /> "tb4" Path="Text" />
注意 MultiBinding 在XAML里面没有智能提示。。。。
原文:https://www.cnblogs.com/AtTheMoment/p/14699827.html