将图像绑定到列表时出现问题

时间:2016-07-21 08:35:55

标签: c# wpf xaml uwp

我需要将ListImage绑定到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>

1 个答案:

答案 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>