【发布时间】: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