深入浅出WPF-06.Binding(绑定)03

MultiBinding(多路Binding)

当UI中的显示信息是由源Source中的多个数据来决定时,使用MultiBinding。他和Binding的区别是需要传递多个元数据,针对多个数据源需要使用一个转换机制类决定目标的属性。

转换器代码:

public class ScanTimeConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        string result = string.Empty;

        if (values == null) return result;
        string strValue = values.ToString();
        if (!string.IsNullOrEmpty(strValue) && !strValue.Equals("NaN") && values.Length == 4)
        {
            try
            {
                ushort RepeatNum = System.Convert.ToUInt16(values[0]);
                ushort FrameNum = System.Convert.ToUInt16(values[1]);
                ushort LineNum = System.Convert.ToUInt16(values[2]);
                ushort Frequency = System.Convert.ToUInt16(values[3]);

                if (Frequency != 0)
                    result = (RepeatNum * FrameNum * LineNum * 1.0 / Frequency).ToString("F2");//单位:ms
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        return result;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

xaml代码:

<TextBlock Margin="0,0,5,0" Style="{StaticResource TextBlockDefault}">
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource ScanTimeConverter}">
            <Binding Path="OCTScanParam.RepeatNum" />
            <Binding Path="OCTScanParam.FrameNum" />
            <Binding Path="OCTScanParam.LineNum" />
            <Binding Path="OCTScanParam.Frequency" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

posted @ 2020-06-19 12:01  蜗牛的希望  阅读(192)  评论(0编辑  收藏  举报