2

我想在每个 ListView 元素之后显示图像。但当然其他图像任何元素。所以,image_1.png 用于第一个 ListView 项,image_2.png 用于第二个 ListView 项等。我想我应该做绑定。我该如何实现?

<ListView x:Name="listView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <!-- IMAGE IS HERE -->
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
</ListView>

编辑(1):

我尝试了一切,但它不起作用。我的代码现在:

型号类:

public ImageSource image { get; set; }

页面类:

BitmapImage bi = new 
bi.UriSource = new Uri("ms-appx:///Logos/x.png");
c.image = bi;

XAML 文件:

<Image Source="{Binding Source=image}"></Image>

为什么它不起作用?

4

2 回答 2

2

您可以像这样绑定到集合中的项目:

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Grid>
        <ListView ItemsSource="{Binding ElementName=UI,Path=YourCollection}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="200" Height="200">
                        <Image Source="{Binding Image}"/>
                        <TextBlock Text="{Binding Title}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
 </Window>

假设类/列表

 /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private ObservableCollection<MyListItem> _yourCollection = new ObservableCollection<MyListItem>();

    public MainWindow()
    {
        InitializeComponent();
        YourCollection.Add(new MyListItem { Title = "Item 1", Image = new BitmapImage(new Uri("C:\\Users\\Dev\\Pictures\\Picture1.PNG", UriKind.RelativeOrAbsolute)) });
        YourCollection.Add(new MyListItem { Title = "Item 2", Image = new BitmapImage(new Uri("C:\\Users\\Dev\\Pictures\\Picture2.PNG", UriKind.RelativeOrAbsolute)) });
    }

    public ObservableCollection<MyListItem> YourCollection
    {
        get { return _yourCollection; }
        set { _yourCollection = value; }
    }
}

public class MyListItem
{
    public string Title { get; set; }
    public BitmapImage Image { get; set; }
}
于 2012-11-26T02:14:18.017 回答
1

我想我可以回答我自己的问题。我找到了解决方案

Image image = new Image();
image.Source = new BitmapImage(new Uri(@"ms-appx:/Assets/Logos/x.png"));

<Image Source="{Binding Path=image.Source.UriSource.AbsolutePath}" Width="55" Height="40"/>
于 2012-11-29T23:59:34.177 回答