问题:XAML中,想要在一个Bingding语句中再次Bingding。

Source="{Binding Path=Image,Converter={StaticResource UMatToBitmapSourceConverter},ConverterParameter={Binding IsMirror}}"

运行后会报错XML解析异常:
【WPF】动态设置Binding的ConverterParameter转换器参数

解决办法:改为使用MultiBinding的及其转换器。

<Image.Source>
    <MultiBinding Converter="{StaticResource UMatToBitmapSourceConverter}">
        <Binding Path="Image"/>
        <Binding Path="IsMirror"/>
    </MultiBinding>
</Image.Source>

多绑定的转换器,构造方法参数中object[] value即是多个Binding携带过来的值。用角标访问这些数据,注意向下转型的类型。

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    bool isMirror = false;
    if (values[1] != null)
    {
        isMirror = (bool)values[1];
    }
    try
    {
        if (values[0] is UMat)
        {
            var image = values[0] as UMat;
            return ChangeBitmapToImageSource(image.Bitmap, isMirror); // do what you want to do here!
        }
        else
        {
            return ChangeBitmapToImageSource(values[0] as System.Drawing.Bitmap);
        }
    }
    catch
    {
        return DependencyProperty.UnsetValue;
    }
}

重要参考:

相关文章:

  • 2021-06-05
  • 2021-12-17
  • 2021-09-28
  • 2022-12-23
  • 2021-05-16
  • 2021-12-05
猜你喜欢
  • 2021-10-16
  • 2022-12-23
  • 2022-03-04
  • 2022-02-26
  • 2022-01-11
  • 2022-02-20
  • 2021-08-07
相关资源
相似解决方案