如何设置 ItemSource 和 ItemTemplate 以显示对象列表

发布于 2024-12-12 11:05:35 字数 1252 浏览 0 评论 0原文

我有一个列表框,我想显示对象列表,我遵循 MVVM 模式,但发现很难实现我想要的。

MainWindowView.xaml

<ListBox ItemsSource="{Binding Path=MyList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding Path=Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

MainWindowViewModel.cs

    private List<ListBoxItem> _myList = new List<ListBoxItem>();

    public List<ListBoxItem> MyList
    {
        get { return _myList ; }
        set
        {
            _myList = value;
            OnPropertyChanged("MyList");
        }
    }

    public SprintBacklogViewModel()
    {
        foreach(MyObject obj in MyObjects.MyObjectList)
        {
            ListBoxItem item = new ListBoxItem();
            item.Content = obj;
            MyList.Add(item);
        }
    }

MyList 已正确更新,但窗口中未显示任何内容。 (ItemsSource="{Binding Path=MyList}" 也有效,我使用不同的数据进行了测试) 我之前没有使用过 ItemTemplate,因此欢迎任何指点。我的理解是,如果我设置正确,它将显示我的对象中的数据。 例如:

<Label Content="{Binding Path=Name}"/>

MyObject 中有一个名为 Name 的属性,我想将其显示为我的列表中的标签

*编辑 在我的窗口中,我得到一行文本 - mynamespace.MyObject

I have a List box that I want to display a list of objects, I am following the MVVM pattern and am finding it difficult to achieve what I want.

MainWindowView.xaml

<ListBox ItemsSource="{Binding Path=MyList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding Path=Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

MainWindowViewModel.cs

    private List<ListBoxItem> _myList = new List<ListBoxItem>();

    public List<ListBoxItem> MyList
    {
        get { return _myList ; }
        set
        {
            _myList = value;
            OnPropertyChanged("MyList");
        }
    }

    public SprintBacklogViewModel()
    {
        foreach(MyObject obj in MyObjects.MyObjectList)
        {
            ListBoxItem item = new ListBoxItem();
            item.Content = obj;
            MyList.Add(item);
        }
    }

MyList is getting updated correctly, but nothing is displaying in the window. (ItemsSource="{Binding Path=MyList}" also works, I tested with different data) I have not used an ItemTemplate before so any pointers are welcome. My understanding of it is that If I set it up correctly It will display the data in my objects.
eg:

<Label Content="{Binding Path=Name}"/>

there is a property in MyObject called Name, I want to display this as a label in my list

*EDIT
In my window I get a line of text - mynamespace.MyObject

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里寻她 2024-12-19 11:05:36

ViewModel 中的 MyList 属性是 ListBoxItem 类型的属性,它具有属性 名称 但它不是名称我的对象。 更改 ViewModel 中的属性

因此,您需要通过Replace

private List<ListBoxItem> _myList = new List<ListBoxItem>();

public List<ListBoxItem> MyList
{
    get { return _myList ; }
    set
    {
        _myList = value;
        OnPropertyChanged("MyList");
    }
}

with

private List<MyObject> _myList = new List<MyObject>();

public List<MyObject> MyList
{
    get { return _myList ; }
    set
    {
        _myList = value;
        OnPropertyChanged("MyList");
    }
}

MyList property in ViewModel is property of type ListBoxItem, it has property Name but it's not Name of MyObject. So you need to change your property in your ViewModel by

Replace

private List<ListBoxItem> _myList = new List<ListBoxItem>();

public List<ListBoxItem> MyList
{
    get { return _myList ; }
    set
    {
        _myList = value;
        OnPropertyChanged("MyList");
    }
}

with

private List<MyObject> _myList = new List<MyObject>();

public List<MyObject> MyList
{
    get { return _myList ; }
    set
    {
        _myList = value;
        OnPropertyChanged("MyList");
    }
}
逐鹿 2024-12-19 11:05:36
  1. 如果您绑定到 ListBoxItems 列表(ListBox数据绑定) code> 将忽略 ItemTemplate 并仅使用适合 ListBox 预期容器的项目。容器将自动生成,您不需要在列表中执行此操作。

  2. 如果您在运行时向集合添加项目,则需要通知绑定引擎更新更改,为此您应该使用 ObservableCollection 或任何实现 INotifyCollectionChanged(这样做时,您通常会进一步使字段只读并且只提供一个getter)这就是没有项目的原因。

  1. Your list should not contain UI-Elements but data (you are data-binding), if you bind to a list of ListBoxItems the ListBox will disregard the ItemTemplate and just use the items as they fit the expected container for the ListBox. Containers will be generated automatically, you do not need to do that in your list.

  2. If you add items to a collection at runtime the binding engine needs to be notified to update the changes, for this you should use an ObservableCollection or anything that implements INotifyCollectionChanged. (When doing so you further usually make the field readonly and only provide a getter) This is the reason why there are no items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文