C# 绑定图像列表
我试图将图像列表(list)绑定到StackPanel,我试图通过使用来分离这些图像,但遗憾的是它不起作用。有人知道为什么吗?(我是wpf的新手……所以sry) 我的代码: 代码隐藏:C# 绑定图像列表,c#,wpf,image,list,binding,C#,Wpf,Image,List,Binding,我试图将图像列表(list)绑定到StackPanel,我试图通过使用来分离这些图像,但遗憾的是它不起作用。有人知道为什么吗?(我是wpf的新手……所以sry) 我的代码: 代码隐藏: List<Image> v2 = new List<Image>(); for (int i = 0; i < 10; i++) { Image v2image = new Image();
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;
List v2=新列表();
对于(int i=0;i<10;i++)
{
图像v2image=新图像();
v2image.BeginInit();
v2image.Source=新位图图像(新Uri(“http://static.lolskill.net/img/champions/64/xayah.png"));
v2图像宽度=40;
v2图像高度=40;
v2.添加(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宽\高
编辑:
这就是它的工作原理:(当我使用一个只有图像的类时,它工作)
这就是它的工作原理:您不应该有一个
列表,而是一个列表
:
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;
不知道分离器应该做什么。在上面的示例中,我只设置了图像的Margin
属性
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Width="40" Height="40" Margin="10"/>
</DataTemplate>
</ItemsControl.ItemTemplate>