【问题标题】:Binding of UriSource to BitmapImage not working将 UriSource 绑定到 BitmapImage 不起作用
【发布时间】:2023-03-15 08:18:02
【问题描述】:

我的代码:

<Image Height="100" Width="100" HorizontalAlignment="Left" VerticalAlignment="Top">
    <Image.Source>
        <BitmapImage DecodePixelWidth="100">
            <BitmapImage.UriSource>
                <PriorityBinding>
                    <Binding Path="MyModel1.ImagePath"/>
                    <Binding Path="MyModel2.ImagePath"/>
                </PriorityBinding>
            </BitmapImage.UriSource>
        </BitmapImage>
    </Image.Source>
</Image>

在我的视图模型中,ImagePath 的值:

public object ImagePath
{
    get { return new Uri("F:/myFolder/default.png", UriKind.Absolute); }
}

路径 F:/myFolder/default.png 存在。我收到错误消息:必须设置属性“UriSource”或属性“StreamSource”。为什么会这样?我在哪里犯错了?

【问题讨论】:

    标签: wpf xaml data-binding bitmapimage


    【解决方案1】:

    只需使用普通的string 文件路径,让框架将它们转换为BitmapImage 元素:

    public string ImagePath
    {
        get { return new "F:/myFolder/default.png"; }
    }
    

    ...

    <Image Height="100" Width="100" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Image.Source>
            <PriorityBinding>
                <Binding Path="MyModel1.ImagePath"/>
                <Binding Path="MyModel2.ImagePath"/>
            </PriorityBinding>
        </Image.Source>
    </Image>
    

    【讨论】:

    • 返回新的“F:/myFolder/default.png”;返回错误:预期类型。
    • 我必须使用 BitmapImage 因为我想使用 DecodePixelWidth 属性。
    • 抱歉,该属性应该是 string... 复制和粘贴错误。我现在已经更新了。
    【解决方案2】:

    问题是,如果您没有立即可用的UriSourceStreamSource,则无法初始化BitmapImage。您没有立即可用的源,因为您通过 Binding 提供它,并且在设置数据上下文并处理绑定之前绑定不可用,这不会立即发生。

    您需要推迟BitmapImage 的创建,直到源可用。您可以使用自定义转换器来做到这一点:

    public class ImageConverter : IValueConverter
    {
        public ImageConverter()
        {
            this.DecodeHeight = -1;
            this.DecodeWidth = -1;
        }
    
        public int DecodeWidth { get; set; }
        public int DecodeHeight { get; set; }
    
        public object Convert(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            var uri = value as Uri;
            if (uri != null)
            {
                var source = new BitmapImage();
                source.BeginInit();
                source.UriSource = uri;
                if (this.DecodeWidth >= 0)
                    source.DecodePixelWidth = this.DecodeWidth;
                if (this.DecodeHeight >= 0)
                    source.DecodePixelHeight = this.DecodeHeight;
                source.EndInit();
                return source;
            }
            return DependencyProperty.UnsetValue;
        }
    
        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
    
    <Image Height="100"Width="100" HorizontalAlignment="Left" VerticalAlignment="Top">
      <Image.Source>
        <PriorityBinding>
          <Binding Path="Model1.ImagePath" Converter="{StaticResource ImageConverter}" />
          <Binding Path="Model2.ImagePath" Converter="{StaticResource ImageConverter}" />
        </PriorityBinding>
      </Image.Source>
    </Image>
    

    ...并将其放入您的资源中:

    <l:ImageConverter x:Key="ImageConverter" DecodeWidth="100" />
    

    【讨论】:

      猜你喜欢
      • 2015-06-26
      • 1970-01-01
      • 2021-11-06
      • 2013-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多