xaml和wpf中的多绑定图像源(multibind image source in xaml & wpf)

编程入门 行业动态 更新时间:2024-10-27 11:19:46
xaml和wpf中的多绑定图像源(multibind image source in xaml & wpf)

在我的项目中,我有一个名为Images的文件夹,其中我在我的应用程序中使用的所有图像都保存在子文件夹中。所有图像都在构建过程中设置为“资源”。

myproject |__Images |__AppImages |__StarOn.png |__StarOff.png

现在,如果我像这样手动设置我的图像:

<Image Source="Images\AppImages\StarOn.png" width="32" height="32"/>

图像正确显示在图像框中。

我想使用转换器和这样的绑定设置图像:

<Image> <Image.Source> <Binding Path="Number" converter="{StaticResource GetImagePathConverter}"/> </Image.Source> </Image>

其中数字是整数

我的转换器是:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int questionNr=int.parse(value.ToString()); if (questionNr>100) { return "Images\\AppImages\\StarOn.png"; } return "Images\\AppImages\\starOff.png"; }

但这不是改变形象?

我做错了什么? 如何从转换器正确设置图像源?

提前致谢

In my project, i have a folder called Images, where all the images iam using in my application are saved in subfolders.All the images are set to "Resource" in the buildprocess.

myproject |__Images |__AppImages |__StarOn.png |__StarOff.png

Now, if i do set my image manually like this:

<Image Source="Images\AppImages\StarOn.png" width="32" height="32"/>

the image is correctly shown in the imagebox.

i would like to set the image using a converter and a binding like this:

<Image> <Image.Source> <Binding Path="Number" converter="{StaticResource GetImagePathConverter}"/> </Image.Source> </Image>

where the number is an integer

and my converter is:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int questionNr=int.parse(value.ToString()); if (questionNr>100) { return "Images\\AppImages\\StarOn.png"; } return "Images\\AppImages\\starOff.png"; }

but this is not changing the image ?..

what iam doing wrong ? how can i set the image source correctly from the converter ?

thanks in advance

最满意答案

您使用转换器的方式不正确。 您需要创建转换器的实例,通过StaticResource在绑定中使用它。 local:是您需要在xaml中声明的本地命名空间 -

<Image> <Image.Resources> <local:GetImagePathConverter x:Key="GetImagePathConverter"/> </Image.Resources> <Image.Source> <Binding Path="Number" Converter="{StaticResource GetImagePathConverter}"/> </Image.Source> </Image>

此外, Source属性不是字符串类型,而是ImageSource因此您需要转换器中的某些内容 -

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int questionNr=int.parse(value.ToString()); if (questionNr>100) { return new BitmapImage(new Uri("Images\\AppImages\\StarOn.png", UriKind.Relative)); } return new BitmapImage(new Uri("Images\\AppImages\\StarOff.png", UriKind.Relative)); }

Your way of using converter is incorrect. You need to create an instance of your converter use it in your binding through StaticResource. local: is the local namespace which you need to declare in your xaml -

<Image> <Image.Resources> <local:GetImagePathConverter x:Key="GetImagePathConverter"/> </Image.Resources> <Image.Source> <Binding Path="Number" Converter="{StaticResource GetImagePathConverter}"/> </Image.Source> </Image>

Also, Source property is not of type string but instead ImageSource so you need something in your converter -

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int questionNr=int.parse(value.ToString()); if (questionNr>100) { return new BitmapImage(new Uri("Images\\AppImages\\StarOn.png", UriKind.Relative)); } return new BitmapImage(new Uri("Images\\AppImages\\StarOff.png", UriKind.Relative)); }

更多推荐

图像,Images,png,converter,设置,电脑培训,计算机培训,IT培训"/> <meta name="de

本文发布于:2023-07-09 00:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   图像   wpf   xaml   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!