WPF image.source绑定数据失败(关于IMultiValueConverter)
有2个参数来控制image的图片位置,我用IMultiValueConverter来转换,结果没显示出图片。
如果,把image换成textbox返回的图片uri可以在显示在text上。有谁知道,什么原因?
下面是代码
<Image.Source>
<MultiBinding Converter="{StaticResource ImageConverter}" Mode="TwoWay">
<Binding Path="one"/>
<Binding Path="two"/>
</MultiBinding>
</Image.Source>
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string one = values[0].ToString();
string two = values[1].ToString();
string strImageUri = string.Empty;
if (one == "1" && two == "2")
{
strImageUri = "/WpfApplication1;Component/images/1-pic-book-1.png";
}
else if (one == "11" && two == "22")
{
strImageUri = "/WpfApplication1;Component/images/1-pic-book-2.png";
}
else
{
strImageUri = "/WpfApplication1;Component/images/1-pic-book-3.png";
}
return strImageUri;
}
你的那个问题应该不是我说的那种情况,你修改下你的Converter,因为你返回的是String的值,string是无法转化为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));
}
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
MemoryStream memStream = new MemoryStream((byte[])value,false);
BitmapImage empImage = new BitmapImage();
empImage.SetSource(memStream);
return empImage;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}