0

イメージ ソース バインディングにコンバーターを適用しようとしています。ここに私のxamlがあります:

<Window.Resources>
        <DataTemplate x:Key="listBoxTemplate">
            <StackPanel Orientation="Horizontal">
                <StackPanel.Resources>
                    <ImageConverter x:Key="MyImageConverter" />
                </StackPanel.Resources>
                <Image Source="{Binding Path=thumb, StringFormat=/WpfTest;component/Images/{0}, Converter={StaticResource MyImageConverter}}" Height="100" Width="130" Margin="5"></Image>
                <StackPanel Orientation="Vertical" Width="247">
                    <TextBlock Text="{Binding recipeName}" Height="60" Padding="15" FontSize="16" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
                    <TextBlock Text="{Binding cuisine}" Height="60" Padding="15" FontSize="16" HorizontalAlignment="Stretch" VerticalAlignment="Center"></TextBlock>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

ここに私のimageConverterクラスがあります:

using System;
using System.Windows.Data;
using System.Globalization;
using System.Windows.Media.Imaging;

public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string path = (string)value;

        try
        {
            //ABSOLUTE
            if (path.Length > 0 && path[0] == System.IO.Path.DirectorySeparatorChar
                || path.Length > 1 && path[1] == System.IO.Path.VolumeSeparatorChar)
                return new BitmapImage(new Uri(path));

            //RELATIVE
            return new BitmapImage(new Uri(System.IO.Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + path));
        }
        catch (Exception)
        {
            return new BitmapImage();
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

しかし、アプリを実行しようとすると、「タイプのイメージコンバーターが見つかりませんでした」が返され、VSがその部分を強調表示します

<ImageConverter x:Key="MyImageConverter" />

上記のxamlで。どうすれば修正できますか?(ところで、Wpfからイメージコンバーターコードを取得しました-相対イメージソースパス

4

1 に答える 1

2

次のように名前空間を追加する必要があります。

<ns:ImageConverter x:Key="MyImageConverter"/>

そして、次のように名前空間をより高い位置に追加したことを確認してください。

<DataTemplate xmlns:ns="....">

実際の名前空間はプロジェクトによって異なりますが、コードの補完が役立ちます。

于 2012-05-21T17:01:04.903 に答える