图像源绑定转换器不返回给定路径

时间:2018-03-24 23:07:30

标签: c# wpf data-binding

我正在尝试使用转换路径中的BitmapImage设置图像源

MainWindow.xaml

            <local:PathToImageConverter x:Key="PathToImageConverter"/>

            <DataTemplate x:Key="GraphicItem">
            <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Height="24" Width="24" HorizontalAlignment="Left" Source="/Handy Clipboard;component/Resources/image.png" />


            <StackPanel Grid.Column="1">
                <TextBlock Text="{Binding VisibleName}">
                    <TextBlock.ToolTip>
                        <Image Source="{Binding Path=ImagePath, Converter={StaticResource PathToImageConverter}}" />
                    </TextBlock.ToolTip>
                </TextBlock>
            </StackPanel>


        </Grid>
    </DataTemplate>

PathToImageConverter类

public class PathToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Console.WriteLine(value as string); // "Images\image name.png"
        return new BitmapImage(new Uri(value as string, UriKind.Relative));
    }

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

DataContext GraphicListItem

public class GraphicListItem : ListItem
{

    public ImageSource Content
    public string ImagePath

    public GraphicListItem() { }
    public GraphicListItem(string visibleName, ImageSource content)
    public GraphicListItem(string visibleName, ImageSource content, string imagePath)
}

我尝试使用转换器绑定Content和ImagePath但结果是相同的 - 空工具提示(2x2 px矩形)

请帮助,我甚至不知道为什么这不起作用

0 个答案:

没有答案