【问题标题】:WPF - Binding to collection in objectWPF - 绑定到对象中的集合
【发布时间】:2011-09-02 03:44:38
【问题描述】:

我试图让它工作几天。 这段代码有什么问题?

这是我的窗口 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 中没有显示任何内容。 列表视图始终为空。

那段代码哪里出错了?

【问题讨论】:

  • 你的清单一开始是空的吗?
  • 不应该绑定到依赖属性,不应该绑定到 observablecollection 吗? msdn.microsoft.com/en-us/library/…
  • 如果不使用 ListView 属性,则不应使用其 View 属性,也不应使用其 ItemTemplate 属性。请改用ListBox,在那里您应该使用所述属性。此外,如果您正确指定 DataType,则您的数据类型缺少所需的 xmlns 前缀。
  • @Daniel Williams 是的,它是空的。
  • 现在见this overview

标签: c# .net wpf data-binding collections


【解决方案1】:

是的,如果您打算在设置 ItemsSource 之后添加到它,它应该是 ObservableCollection&lt;DownloadLink&gt;。如果列表是预加载的并且您不会更改它,List&lt;T&gt; 会起作用。

现在我确实认为

MainListBox.ItemsSource = RapideoAccount.Links;

在技术上仍然是一个绑定。但是您可能想到的是通过 DataContext 而不是直接绑定(al la MVVM 风格)。那就是:

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

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

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

【讨论】:

  • 谢谢。这正是我想要的。
【解决方案2】:

我认为 Links 需要是 ObservableCollection,而不是 List。

【讨论】:

  • 将所有内容更改为 ObservableCollection 后仍然无法正常工作。
  • 我正在尝试重现它,但不能 - 我的代码有效。 DownloadLink 是公共课程吗?如果我将我设为非公开(默认为私有),那么我什至无法编译代码。
  • 啊哈 - 如果您只是更新列表,它不会做任何事情。您需要重新分配: MainListBox.ItemsSource = RapideoAccount.Links;更新链接后。这不是真正的绑定,但会完成。另外——你可以使用一个列表——不需要 Observable 集合。绑定需要更多...
【解决方案3】:

首先,如果您打算在设置绑定后更改列表,则应使用ObservableCollection&lt;DownloadLink&gt; 而不是List&lt;DownloadLink&gt;

其次,要明确一点:

MainListBox.ItemsSource = RapideoAccount.Links;

不是绑定。你只是在设置属性。这适用于某些场景,但它并不是我们通常在 WPF 中谈论的真正的绑定。

【讨论】:

  • @Hooch:在 XAML 中:ItemsSource="{Binding Path=RapidioAccounts.Links}"。这将假设您的 DataContext 具有 RapidioAccounts 属性。如果没有,那么您还需要设置Source
猜你喜欢
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
  • 2010-12-10
相关资源
最近更新 更多