我不能绑定图像源

本文关键字:图像 绑定 不能 | 更新日期: 2023-09-27 18:17:52

这里是绑定:

<Image Width="16" Height="16"  Source="{Binding SwitchForImage, Converter={StaticResource stringToImage}}" HorizontalAlignment="Left">
</Image>

这是转换器

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string type = (string)value;
        BitmapImage logo = new BitmapImage();
        logo.BeginInit();
        logo.UriSource = new Uri(@"pack://application:,,,/Resources/"+type+@"Icon.png");
        logo.EndInit();
        return logo;
    }

当我运行时,给出这个异常"无法定位资源'resources/*icon.png' "。但是我把png文件放在名为资源的文件夹中。我正在创建图书馆。这些都在图书馆里。为了测试,我使用了它,然后出现了这个问题。

我添加了png文件作为"包含到项目"。构建动作是"内容"。但我也试过其他的(资源,嵌入式资源)

(答案)我将Build Action更改为Resource,并将Convert更改为:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return "/AutoComplete;component/Resources/" + (string)value + "Icon.png";
    }

一切正常

我不能绑定图像源

我将Build Action更改为Resource,并将Convert方法更改为:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    return "/AutoComplete;component/Resources/" + (string)value + "Icon.png";
}