WPF:列表框中的图像路径,显示图像代替路径

发布于 2024-10-19 08:45:53 字数 860 浏览 3 评论 0原文

我有一个列表框,其中填充了不同图像的路径。我将如何更改 ItemTemplate 以便显示图像而不是路径(字符串)。

这是代码:

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Height="50" Width="50" Source="{Binding Path=Content}" Stretch="Fill"></Image>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0083A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0102A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0103A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0104A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0105A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0106A.jpg</ListBoxItem>
</ListBox>

I have a ListBox filled with paths of different images. How will I alter the ItemTemplate so that the images will be shown instead of paths(string).

Here is the code:

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Image Height="50" Width="50" Source="{Binding Path=Content}" Stretch="Fill"></Image>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0083A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0102A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0103A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0104A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0105A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0106A.jpg</ListBoxItem>
</ListBox>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撑一把青伞 2024-10-26 08:45:53

您可以制作一个 IValueConverter 将字符串转换为 ImageSource。

类似于:

public class ImagePathConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
      return new BitmapImage(new Uri(value as string));
  }
  public object ConvertBack(xxx) { throw new NotSupportedException(); }
}

然后创建一个值转换器资源并在您的绑定中使用它。

资源可以这样定义:

<UserControl.Resources>
   <myNameSpaceAlias:ImagePathConverter x:Key="ImagePathConverter"/>
...

然后绑定:

{Binding Path=Content, Converter={StaticResource ImagePathConverter}}

You could make an IValueConverter that converts a string to a ImageSource.

Something like:

public class ImagePathConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
      return new BitmapImage(new Uri(value as string));
  }
  public object ConvertBack(xxx) { throw new NotSupportedException(); }
}

And then create a value converter resource and use that in your binding.

a resource could be defined like:

<UserControl.Resources>
   <myNameSpaceAlias:ImagePathConverter x:Key="ImagePathConverter"/>
...

and then bind with:

{Binding Path=Content, Converter={StaticResource ImagePathConverter}}
从来不烧饼 2024-10-26 08:45:53

在 UI 生成期间,ListBoxItemTemplate 被复制到 ListBoxItemContentTemplate。但是,直接添加 ListBoxItem 时,对于已属于 ItemsControl 容器类型的项目(ListBoxItem),将忽略 ItemTemplate > 用于 ListBoxListViewItem 用于 ListView 等)。因此,在这种情况下,您必须直接使用 ItemContainerStyle 的 ContentTemplate。

另外,将 Source="{Binding Content}" 更改为 Source="{Binding}"

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Image Height="50" Width="50" Source="{Binding}" Stretch="Fill"></Image>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0083A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0102A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0103A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0104A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0105A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0106A.jpg</ListBoxItem>
</ListBox>

The ItemTemplate of a ListBox is copied to the ContentTemplate of a ListBoxItem during UI generation. However, when adding the ListBoxItems directly, ItemTemplate is ignored for items already of the ItemsControls container type (ListBoxItem for ListBox, ListViewItem for ListView etc.). So in this case, you'll have to use the ContentTemplate of the ItemContainerStyle directly instead.

Also, change Source="{Binding Content}" to Source="{Binding}"

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Image Height="50" Width="50" Source="{Binding}" Stretch="Fill"></Image>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0083A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0102A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0103A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0104A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0105A.jpg</ListBoxItem>
    <ListBoxItem>C:\Users\AKSHAY\Pictures\IMG0106A.jpg</ListBoxItem>
</ListBox>
老旧海报 2024-10-26 08:45:53

您必须在绑定中使用值转换器并传递位图图像

You have to use a value converter in the binding and pass a bitmap image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文