我正在尝试在列表框中获取图像,提供iMultiValueConverter的路径,在后台线程中执行:
<Image>
<Image.Source>
<MultiBinding Converter="{StaticResource UriToBitmapConverter}">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}" />
<Binding Source="{Binding ImagePath}" />
</MultiBinding.Bindings>
</MultiBinding>
</Image.Source>
</Image>
和
public class UriToBitmapConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values[1] == null) return null;
BitmapImage BI= null;
Image image = values[0] as Image;
string mypath = values[1] as string;
FileStream fileStream = new FileStream(mypath, FileMode.Open, FileAccess.Read);
ThreadPool.QueueUserWorkItem((WaitCallback)delegate
{
image.Dispatcher.Invoke(DispatcherPriority.Normal,
(ThreadStart)delegate
{
BI= new BitmapImage();
BI.BeginInit();
BI.StreamSource = fileStream;
BI.EndInit();
image.Source = BI;
});
});
return BI;
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception("The method or operation is not implemented.");
}
}
我明白了:
未处理的类型异常 'System.Windows.Markup.XamlParseException'发生在 PresentationFramework.dll
附加信息:无法在'来源'上设置'绑定' '绑定'类型的属性。 '绑定'只能在a上设置 DependencyObject的DependencyProperty。
你能告诉我我做错了什么吗?