【发布时间】:2011-05-10 21:33:44
【问题描述】:
我敢肯定,对于那些有 WPF 数据绑定经验的人来说,这是一个非常菜鸟的问题,但我是菜鸟。所以提前道歉:
我正在尝试将对象集合 (IEnumerable) 绑定到列表框,如下所示。问题是,当我的 LogEntry 类型与我的“代码隐藏”在同一个命名空间 (ActivityLog) 中时,我可以在列表框中看到对象的属性。 然而;当 LogEntry 类型位于不同的命名空间 (ActivityLog.Classes) 中时,列表框中不会显示任何内容。
我已尝试在 xaml 标记中添加 ActivityLog.Classes 命名空间 (xmlns:local="clr-namespace:ActivityLog.Classes"),但我确信我缺少一些额外的步骤。
请帮忙。
namespace ActivityLog
{
public partial class LogPage : Window
{
public LogPage()
{
InitializeComponent();
List<LogEntry> leList = new List<LogEntry>() { new LogEntry() { StartDate=DateTime.Parse("2011-05-10 9:58:00"), ActivityDescription="Three" + Environment.NewLine},
new LogEntry() { StartDate=DateTime.Parse("2011-05-10 9:58:00"), ActivityDescription="Four" + Environment.NewLine}};
this.lstBox.ItemsSource = leList;
}
}
public class LogEntry
{
public DateTime StartDate { get; set; }
public string ActivityDescription { get; set; }
}
}
<Window x:Class="ActivityLog.LogPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ActivityLog.Classes"
Title="LogPage" Height="564" Width="414" >
<Grid>
<ListBox Width="361" Margin="20,50,0,0" ItemsSource="{Binding}" Name="lstBox" Height="429" VerticalAlignment="Top" HorizontalAlignment="Left">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=StartDate}"/>
<TextBlock Text="{Binding Path=ActivityDescription}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
【问题讨论】:
-
在滥用代码之前请阅读formatting help。
-
抱歉,正在尝试格式化。
-
好吧,除非你知道自己在做什么,否则请停下来。
标签: wpf data-binding namespaces