0

バインディングを介してアプリケーションのソースとしてxmlを使用しています。xmlには、フォルダーのリストと各フォルダーのサンプル画像のパスがあります。フォルダリストはリストボックスにバインドされ、別の表示がリストボックスの選択されたアイテムにバインドされます。これはxmlリストのアイテムです(タイプXmlNode)。XmlProviderによってxmlからコピーされたXmlDocumentを使用してアイテムを追加および削除する機能を追加し、ソースファイルに保存しました。

問題は、アプリケーションのロード時、またはすべてのアイテムを削除した後のいずれかで、ソースリストが空のときに始まります。この時点で、表示のバインドされた値はすべてnullになります。何も表示されないcanvasbackgroundimagebrush image_sourceプロパティを除いて、バインディングのTargetNullValueプロパティを使用してすべてのバインディングを解決しました。

コンバーターを使おうとしましたが、デバッグしてみると変なところがありました。リストに項目がある場合、コンバーターは本来あるべきものを返し、画像が表示されましたが、リストが空の場合、コンバーターは本来あるべきものを返し、画像は表示されませんでした。plzは私を助けます。

コード:

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;
    }

}
4

1 に答える 1

0

null をチェックするに、いずれかの値に対してメソッドを呼び出します。

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

これは、考えられるエラーの原因の 1 つにすぎません。もちろん、Kent Boogaart がすでに指摘したように、ファイル パスは正しい必要があります。さらに、「これは機能しません」は役に立ちません。適切な回答が必要な場合は、できるだけ多くの情報を提供してください。つまり、正確に何が起こったのか、何を期待していたのか、それらの期待がどのように満たされていないのか。


ちなみに、コンバーターは次のように圧縮できます。

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

おそらくまだ理想的ではありませんが、散らかっていません。


編集:コードが機能するので、レイアウトが原因であると思われます。アイテムがない場合、コントロールがスペースを占有しない可能性があるため、画像が見えなくなります。

于 2011-10-29T20:02:55.347 に答える