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[] == )
{
return "--";
}
else
{
double bianhua = Math.Abs(((double)values[] - (double)values[]) / (double)values[]);
string result = (bianhua * ).ToString("") + "%";
return result;
}
}
catch(Exception)
{
return "--";
}
} public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return (value as string).Split(' ');
}
}
04-17 09:04