【问题标题】:WPF Markup Not Seeing Type in a Different NamespaceWPF 标记在不同的命名空间中看不到类型
【发布时间】: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


【解决方案1】:

您将两次设置 ListBox 的 ItemSource。一旦进入你的代码隐藏

this.lstBox.ItemsSource = leList;

然后再次在您的 xaml 中

ItemsSource="{Binding}"

在提供的示例中,只有代码隐藏版本是正确的,您应该删除 xaml 版本。

您的 xaml 版本实际上绑定到 LogPage 的 DataContext 属性,因此如果您想以这种方式连接它,您的代码隐藏应该设置

this.DataContext = leList;

【讨论】:

  • 确认 ItemSource="{Binding}" 是多余的。但是,如果 LogEntry 类位于不同的命名空间中(与代码隐藏类不同),则列表框仍然不会呈现任何项目。
  • 这不是我的经验,所以你还有其他事情要做。那么删除 xaml 并没有解决它?
  • 没有。即使将 LogEntry 类封装在不同的命名空间中,您的工作是否也有效?在原始示例中,它与代码隐藏类(即:ActivityLog)位于同一空间中,因此它应该按原样工作。但是将 LogEntry 放入一个单独/不同的命名空间(例如:ActivityLog.Classes)是它对我来说破坏它的地方,并且不会在列表框中显示任何内容。
  • 是的,它在这里工作,我将您的代码副本复制到一个新项目中,按原样工作。将 LogEntry 移至新命名空间并继续工作。
【解决方案2】:

绑定文本框的行应该是:

Text="{Binding Path=(local:StartDate)}"
Text="{Binding Path=(local:ActivityDescription)}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 2016-09-02
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    相关资源
    最近更新 更多