WPF-绑定到对象中的集合 [英] WPF - Binding to collection in object

查看:81
本文介绍了WPF-绑定到对象中的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使它工作几天。
此代码有什么问题?

I was trying to get it working for few days. What is wrong in this code?

这是我的窗口XAML:

This is my window XAML:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Rapideo_Client"
        x:Class="Rapideo_Client.MainWindow"     
        Title="NVM" SnapsToDevicePixels="True" Height="400" Width="625">
    <Window.Resources>
        <DataTemplate x:Key="linksTemplate" DataType="DownloadLink">
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"></TextBlock>
                <Label Content="{Binding Path=SizeInMB}"/>
                <Label Content="{Binding Path=Url}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources> 
        <ListView   ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    ScrollViewer.VerticalScrollBarVisibility="Visible"
                    x:Name="MainListBox"
                    ItemTemplate="{DynamicResource linksTemplate}">                
        </ListView>
</Window>

这是我的课程:

class Rapideo
    {
        (...)
        public List<DownloadLink> Links { get; private set; }
        (...)
    }

这是我的项目:

class DownloadLink
{
    public string Name { get; private set; }
    public string Url { get; private set; }
    public DateTime ExpiryDate { get; private set; }
    public float SizeInMB { get; private set; }
    public int Path { get; private set; }
    public string Value { get; private set; }
    public LinkState State { get; set; }
    public enum LinkState
    {
        Ready, Downloading, Prepering, Downloaded
    }

    public DownloadLink(string name, string url, DateTime expiryDate, float sizeInMB, int path, string value, LinkState state)
    {
        Name = name;
        Url = url;
        ExpiryDate = expiryDate;
        SizeInMB = sizeInMB;
        Path = path;
        Value = value;
        State = state;
    }
}

这是我的约束力:

RapideoAccount = new Rapideo();
MainListBox.ItemsSource = RapideoAccount.Links;

稍后在代码中,我在RapideoAccount.Links中填充了该列表。
但ListView中什么都没有显示。
列表视图始终为空。

Later in the code I populate that list in RapideoAccount.Links. But nothing is showing in ListView. List View is always empty.

该代码在哪里出错?

推荐答案

是的,如果您打算在设置之后添加到其中,它应该是 ObservableCollection< DownloadLink> ItemsSource 。如果列表是预加载的,并且您不会进行更改,则 List< T> 会起作用。

Yes, it should be an ObservableCollection<DownloadLink> if you're planning on adding to it AFTER you have setup the ItemsSource. If the list is preloaded and you won't be changing it, List<T> would have worked.

现在,我确实认为

MainListBox.ItemsSource = RapideoAccount.Links;

仍然具有约束力。但是您可能会想到的是通过DataContext绑定而不是直接绑定(例如MVVM风格)。这样就可以了:

is still technically a binding. But what you are probably thinking of is binding via the DataContext rather than directly (al la MVVM style). So that'd be:

RapideoAccount = new Rapideo();
this.DataContext = RapideoAccount;

然后在窗口中,像这样绑定ItemSource:

Then in your window, you'd bind your ItemSource like this:

<Window
    ...
    <ListView  ScrollViewer.HorizontalScrollBarVisibility="Disabled"
               ScrollViewer.VerticalScrollBarVisibility="Visible"
               x:Name="MainListBox"
               ItemsSource="{Binding Links}" 
               ItemTemplate="{DynamicResource linksTemplate}">                
    </ListView>
</Window>

这篇关于WPF-绑定到对象中的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆