Imagebrush图像源绑定转换器 [英] Imagebrush image source binding converter

查看:144
本文介绍了Imagebrush图像源绑定转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xml作为我的应用程序的来源通过绑定。在xml中有一个文件夹列表和每个文件夹的示例图像路径。文件夹列表绑定到列表框,另一个显示绑定到列表框的所选项目,该列表框是xml列表(类型XmlNode)的项目。我添加了使用从XmlProvider从xml复制的XmlDocument来添加和删除项目的opertunity,并将其保存到源文件。



问题始于源列表为空,无论是在应用程序加载时还是在删除所有项目之后。此时显示的所有绑定值都为空。我解决了所有绑定的绑定的TargetNullValue属性,exept的canvas背景imagebrush image_source属性没有显示。



我试图使用一个转换器,但是当我调试它我看到一些奇怪的东西如果列表中有项目,则转换器返回应该显示的内容和图像,但如果列表为空,则转换器返回应该显示的内容,并且没有显示图像!
plz帮助我。



代码:



XML:

 <文件夹> 
<文件夹ID =1>
< Path> folder3\1< / Path>
< SampleImage> C:\images\2011-09-22\site3\1\6.jpg< / SampleImage>
< / Folder>
< / Folders>

XAML:

 code>< Canvas.Background> 
< ImageBrush x:Name =SampleImageStretch =Uniform>
< ImageBrush.ImageSource>
< MultiBinding Converter ={StaticResource ImageConverter}Mode =OneWay>
<绑定XPath =./ SampleImage/>
< Binding Source =C:\images\SampleImages\\\
o_image.jpg/>
< / MultiBinding>
< /ImageBrush.ImageSource>
< / ImageBrush>
< /Canvas.Background>

c#:

 code> public class ImageConverter:IMultiValueConverter 
{
public object Convert(object [] value,Type targetType,object parameter,System.Globalization.CultureInfo culture)
{
ImageSourceConverter imageConverter = new ImageSourceConverter();
bool bool1 = value [0] .Equals(DependencyProperty.UnsetValue);
if(value [0]!= null&&bool1)//如果源不为空
{
//这样工作很好
return imageConverter。 ConvertFromString(value [0] .ToString());
}
//这里转换器返回正确的对象,但替代图像不显示,背景留空
return imageConverter.ConvertFromString(value [1] .ToString());

//这里也是转换器返回正确的对象,但替代图像不显示,背景留空
// return imageConverter.ConvertFromString(@C:\images\\ \\SampleImages\\\
o_image.jpg);

}


public object [] ConvertBack(object value,Type [] targetType,object parameter,System.Globalization.CultureInfo culture)
{
返回null;
}

}


解决方案

您可以在 之前调用其中一个值的方法:

  bool bool1 = value [0] .Equals(DependencyProperty.UnsetValue); 
if(value [0]!= null&&bool1)

只是一个可能的错误来源。当然,文件路径需要正确,肯特博加塔已经指出了。进一步:这不工作 有帮助,如果你想要好的答案提供尽可能多的信息。即发生了什么事&您的期望以及这些期望如何不符合。






转换器可以通过以下方式压缩到以下内容: / p>

  string path =(value [0] is string&& value [0]!= null)? 
(string)value [0]:(string)value [1];
返回新的ImageSourceConverter()。ConvertFromString(path);

可能还不太理想,但不那么混乱。






编辑:由于代码适用于我,我怀疑布局是怪的,如果没有您的控件可能不占用的项目任何空间,因此使图像不可见。


I'm using an xml as source of my application via binding. in the xml there is a list of folders and a path for a sample image for each folder. The folder list is binded to listbox, and another display is binded to the selected item of the listbox, which is an item of the xml list (type XmlNode). I added the opertunity to add and remove items using the XmlDocument which was copied from the xml by the XmlProvider and save it to the source file.

The problem begins when the source list is empty, either at the application load time, or after removing all of the items. at this point all of the binded values of the display are null. I solved all of the bindings with the binding's TargetNullValue property, exept the canvas background imagebrush image_source property which shows nothing.

I tried to use a converter, but when I debuged it I saw something weird. if there were items in the list the converter returned what is should and the image was displayed, but if the list was empty, the converter returned what it should and no image was shown! plz help me.

Code:

XML:

  <Folders>
    <Folder Id="1">
      <Path>folder3\1</Path>
      <SampleImage>C:\images\2011-09-22\site3\1\6.jpg</SampleImage>
    </Folder>
  </Folders>

XAML:

    <Canvas.Background>
      <ImageBrush x:Name="SampleImage" Stretch="Uniform" >
        <ImageBrush.ImageSource>
          <MultiBinding Converter="{StaticResource ImageConverter}" Mode="OneWay">
            <Binding XPath="./SampleImage" />
            <Binding Source="C:\images\SampleImages\no_image.jpg"/>
          </MultiBinding>
        </ImageBrush.ImageSource>
      </ImageBrush>
   </Canvas.Background>

c#:

public class ImageConverter : IMultiValueConverter
{
    public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ImageSourceConverter imageConverter = new ImageSourceConverter();
        bool bool1=value[0].Equals(DependencyProperty.UnsetValue);
        if (value[0] != null &&!bool1) //if the source isn't null
        {
             //this works fine
             return imageConverter.ConvertFromString(value[0].ToString());
        }
        //here the converter returns the right object but the alternate image isn't shown and the background left blank
        return imageConverter.ConvertFromString(value[1].ToString());

        //here too the converter returns the right object but the alternate image isn't shown and the background left blank
        //return imageConverter.ConvertFromString(@"C:\images\SampleImages\no_image.jpg");

    }


    public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

}

解决方案

You call a method on one of the values before checking it for null:

bool bool1=value[0].Equals(DependencyProperty.UnsetValue);
if (value[0] != null &&!bool1)

This is just one possible source for errors. Of course the file paths need to be correct as well as Kent Boogaart already pointed out. Further: "this don't work" is not helpful, if you want good answers provide as much information as possible. i.e. what exactly happened & what you expected and how those expectations were not met.


The converter could be compressed to the following by the way:

string path = (value[0] is string && value[0] != null) ?
    (string)value[0] : (string)value[1];
return new ImageSourceConverter().ConvertFromString(path);

Quite possibly still not ideal but less cluttered.


Edit: As the code works for me i suspect that the layout is to blame, if there are no items your control possibly does not take up any space anymore, hence making the image invisible.

这篇关于Imagebrush图像源绑定转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆