【问题标题】:WPF - List items not visible in Blend when 'DisplayMemberPath' is usedWPF - 使用“DisplayMemberPath”时,列表项在 Blend 中不可见
【发布时间】:2010-03-24 16:52:53
【问题描述】:

我们目前正在研究如何实现 MVVM,并且我已经在 blend 中设置了 MVVM Light Toolkit,并且可以指定在 Blend 中运行时要提供的虚拟数据。都很好。

我创建了一个虚拟数据列表。该列表包含一个名为 DummyItem 的非常简单的类的 6 个实例,该类具有 Age 和 Name 属性。

这是我的“DummyList”类的主要代码:

public class DummyItem{

    public string Name;
    public int Age;

    public DummyItem(string name, int age){
        this.Name = name;
        this.Age = age;
    }
}

public class DummyList : ArrayList
{
    public DummyList()
    {
        this.Add(new DummyItem("Dummy1", 00));
        this.Add(new DummyItem("Dummy2", 01));
        this.Add(new DummyItem("Dummy3", 02));
        this.Add(new DummyItem("Dummy4", 03));
        this.Add(new DummyItem("Dummy5", 04));
        this.Add(new DummyItem("Dummy6", 05));
    }
}

这是我的 XAML 的操作部分。 DataContext 行确实有效并指向正确的 ViewModel。

<Grid x:Name="LayoutRoot">
    <ListBox x:Name="ListViewBox"
        DataContext="{Binding Source={StaticResource Locator}, Path=ListViewModel}"
        ItemsSource="{Binding TheList}"
        DisplayMemberPath="Name">
    </ListBox>
</Grid>

问题是,当我像上面那样添加“DisplayMemberPath”时,我再也看不到 Blend 中的列表项。如果我删除“DisplayMemberPath”,那么我会看到一个对象列表 (DummyItem) 及其完整路径。当应用程序运行时,一切正常。只是在 Blend 本身中,当我使用“DisplayMemberPath”时,我看不到列表项。

有人知道为什么我在使用 DisplayMemberPath 时看不到 Blend 本身内部的项目吗?

谢谢!

【问题讨论】:

    标签: wpf mvvm binding listbox expression-blend


    【解决方案1】:

    尝试制作姓名和年龄属性:

    public class DummyItem
    {  
        public string Name {get; set; }
        public int Age {get; set;}
    
        public DummyItem(string name, int age)
        { 
            this.Name = name; 
            this.Age = age; 
        } 
    }
    

    【讨论】:

    • 完全正确。没有得到;放;意味着他们没有被揭露。我没有意识到你必须放 get;放;即使他们是空的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多