相关疑难解决方法(0)

使用http:\\ URL的WPF图像UriSource和数据绑定

我在WPF用户控件中显示带有Web URL的图像时遇到问题.我已经解决了2008年8月在本网站上提出的类似问题的所有建议(图像UriSource和数据绑定),但这些建议都没有奏效.

我想做的是:

<Image Width="50" Name="MemberImage">
    <Image.Source>
        <BitmapImage DecodePixelWidth="50" UriSource="{Binding Member.ImageFilePathUri}" />
    </Image.Source>
</Image>
Run Code Online (Sandbox Code Playgroud)

ImageFilePathUri是一个从字符串路径创建的Uri:

public Uri ImageFilePathUri
    {
        get
        {
            return new Uri(this.ImageFilePath);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这样就必须设置"Property'UriSource'或属性'StreamSource'." 错误如预期.

我也尝试过使用值转换器:

public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var image = new BitmapImage();
        image.BeginInit();
        if (value != null)
        {
            image.UriSource = new Uri((string)value);
        }
        image.DecodePixelWidth = 50;
        image.EndInit();
        return image;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,使用以下方法绑定它:

<Image Name="TestImage" Width="50" Source="{Binding Path=Member.ImageFilePath, Converter=Parliament.HansardApplicationSuite.Logging.Helpers.ImageConverter}"></Image> …
Run Code Online (Sandbox Code Playgroud)

c# data-binding url wpf xaml

7
推荐指数
1
解决办法
3万
查看次数

标签 统计

c# ×1

data-binding ×1

url ×1

wpf ×1

xaml ×1