绑定图像列表

问题描述 投票:0回答:1

我试图将图像列表(列表)绑定到 StackPanel,我尝试使用 <

Separator
> 来分隔这些图像,但遗憾的是它不起作用。有人知道为什么吗? (我是 wpf 的菜鸟..所以抱歉)

我的代码: 背后代码:

            List<Image> v2 = new List<Image>();
        for (int i = 0; i < 10; i++)
        {
            Image v2image = new Image();
            v2image.BeginInit();

            v2image.Source = new BitmapImage(new Uri("http://static.lolskill.net/img/champions/64/xayah.png"));
            v2image.Width = 40;
            v2image.Height = 40;
            v2.Add(v2image);
        }

        BlueTeam.ItemsSource = v2;

XAML:

<Window x:Class="FML.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:FML"
    mc:Ignorable="d"
    Title="MainWindow" Height="525.885" Width="809.974">
<Grid>
    <ItemsControl Grid.Column="0" x:Name="BlueTeam">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" >

                </StackPanel>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical" >
                    <Image Source="{Binding v2image.Source}"></Image>
                    <Separator Opacity="0" Height="20" Width="20"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>   
    </ItemsControl>
</Grid>

感谢您帮助我:D 顺便说一句:图像也很小。它们的宽度\高度不是 40

编辑: 这就是它应该如何工作:https://i.stack.imgur.com/Snrre.jpg(当我使用只有图像的类时它可以工作)

这就是它的工作原理:https://i.stack.imgur.com/mU4WN.jpg

c# wpf image list binding
1个回答
0
投票

您不应该有

List<Image>
,而应该有
List<ImageSource>

var v2 = new List<ImageSource>();

for (int i = 0; i < 10; i++)
{
    v2.Add(new BitmapImage(
        new Uri("http://static.lolskill.net/img/champions/64/xayah.png")));
}

BlueTeam.ItemsSource = v2;

然后,您可以在 ItemTemplate 中声明一个具有固定大小的 Image 控件,并将其直接绑定到集合元素,通过

Source="{Binding}"
:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Image Source="{Binding}" Width="40" Height="40" Margin="10"/>
    </DataTemplate>
</ItemsControl.ItemTemplate>   

不确定分离器应该做什么。在上面的示例中,我只是设置了图像的

Margin
属性。

© www.soinside.com 2019 - 2024. All rights reserved.