在我的项目中,我有一个名为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.pngNow, 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
发布评论