我需要将List
与Image
绑定到ListView
,但它无法正常工作
我的班级List
public Image FrameImage { get; set; } = new Image();
public string Path { get; set; }
private static List<FramesViewModel> framesCollection = new List<FramesViewModel>
{
new FramesViewModel() { FrameImage = {Source = new BitmapImage(new Uri("ms-appx:/Frames/Frame1.png"))}, Path = @"Frames\Frame1.png"},
};
public static List<FramesViewModel> FramesCollection => framesCollection;
和我的XAML
<ListView
ItemsSource="{Binding FramesCollection}"
DataContext="{StaticResource FramesViewModel}">
<DataTemplate>
<Border
Width="250"
Height="150">
<Border.Background>
<ImageBrush Stretch="Fill"
ImageSource="{Binding FrameImage}"/>
</Border.Background>
</Border>
</DataTemplate>
</ListView>
答案 0 :(得分:0)
在您的XAMl中,您错过了ListView.ItemTemplate
包裹DataTemplate
我的例子
class FramesViewModel
{
public BitmapImage FrameImage { get; set; } = new BitmapImage();
private static List<FramesViewModel> framesCollection = new List<FramesViewModel>
{
new FramesViewModel() { FrameImage = new BitmapImage(new Uri("http://www.citgroup.in/images/icon/wcf.png"))},
};
public static List<FramesViewModel> FramesCollection => framesCollection;
}
和
<Window.Resources>
<local:FramesViewModel x:Key="FramesVM" />
</Window.Resources>
<Grid>
<ListView
ItemsSource="{Binding FramesCollection}"
DataContext="{StaticResource FramesVM}">
<ListView.ItemTemplate>
<DataTemplate>
<Border
Width="250"
Height="150">
<Border.Background>
<ImageBrush Stretch="Fill"
ImageSource="{Binding FrameImage}"/>
</Border.Background>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>