不使用 XAML 将子项添加到 ListView

发布于 2024-10-12 09:23:34 字数 221 浏览 4 评论 0原文

如何向 ListView 添加子项?我需要动态生成所有内容,但我找到的每个示例都使用 XAML。

非 WPF 是如此简单:

ListViewItem lvi = listview.items.add(wahtever);
lvi. blah blah blah

如何在不使用 XAML 的情况下在 WPF 中添加子项?

How do you add sub-items to a ListView? I need to generate everything dynamically, but every example I've found uses XAML.

Non-WPF was so simple:

ListViewItem lvi = listview.items.add(wahtever);
lvi. blah blah blah

How do you add sub-items in WPF without using XAML?

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

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

发布评论

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

评论(3

深白境迁sunset 2024-10-19 09:23:34

正如已经提到的,WPF 没有像 WinForms 这样的子项。相反,您可以使用适合您目的的对象的属性。

为了完整起见,下面是 XAML 与代码的对比。

XAML:

    <UniformGrid Columns="2">
        <ListView Name="xamlListView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="X Value" DisplayMemberBinding="{Binding X}"/>
                    <GridViewColumn Header="Y Value" DisplayMemberBinding="{Binding Y}"/>
                </GridView>
            </ListView.View>
            <ListView.Items>
                <PointCollection>
                    <Point X="10" Y="20"/>
                    <Point X="20" Y="30"/>
                </PointCollection>
            </ListView.Items>
        </ListView>
        <ListView Name="codeListView"/>
    </UniformGrid>

代码:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var view = new GridView();
        view.Columns.Add(new GridViewColumn { Header = "First Name", DisplayMemberBinding = new Binding("First") });
        view.Columns.Add(new GridViewColumn { Header = "Last Name", DisplayMemberBinding = new Binding("Last") });
        codeListView.View = view;
        codeListView.Items.Add(new { First = "Bill", Last = "Smith" });
        codeListView.Items.Add(new { First = "Jane", Last = "Doe" });
    }

As already mentioned, WPF doesn't have sub-items like WinForms. Instead you use properties on an object that suits your purposes.

For completeness, here is XAML contrasted with code.

XAML:

    <UniformGrid Columns="2">
        <ListView Name="xamlListView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="X Value" DisplayMemberBinding="{Binding X}"/>
                    <GridViewColumn Header="Y Value" DisplayMemberBinding="{Binding Y}"/>
                </GridView>
            </ListView.View>
            <ListView.Items>
                <PointCollection>
                    <Point X="10" Y="20"/>
                    <Point X="20" Y="30"/>
                </PointCollection>
            </ListView.Items>
        </ListView>
        <ListView Name="codeListView"/>
    </UniformGrid>

Code:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var view = new GridView();
        view.Columns.Add(new GridViewColumn { Header = "First Name", DisplayMemberBinding = new Binding("First") });
        view.Columns.Add(new GridViewColumn { Header = "Last Name", DisplayMemberBinding = new Binding("Last") });
        codeListView.View = view;
        codeListView.Items.Add(new { First = "Bill", Last = "Smith" });
        codeListView.Items.Add(new { First = "Jane", Last = "Doe" });
    }
萧瑟寒风 2024-10-19 09:23:34

“WPF 方式”是将列表视图绑定到表示要显示的数据的集合。然后将包含数据的对象添加到该集合中。您几乎不需要像您计划的那样手动将 ListViewItems 添加到列表中。我可以举一个例子,但是这里已经有很多线程可以解决这个问题:

  1. 以编程方式将 ListViewItem 添加到 WPF 中的 Listview
  2. WPF ListView - 如何以编程方式添加项目?

The "WPF way" would be to bind your listview to a collection that represents the data you want to display. Then add objects containing data to that collection. You almost never should have to deal with adding ListViewItems to your list manually as you are planning to do. I could come up with an example but there's many threads here on SO already that solve exactly this problem:

  1. Add programmatically ListViewItem to Listview in WPF
  2. WPF ListView - how to add items programmatically?
笨笨の傻瓜 2024-10-19 09:23:34

子项还是列表项??
简单:

myListbox.Items.Add(object);

您可以将任何类型的对象添加到该集合或特定的 ListBoxItem 对象中,如下所示:

this.List.Items.Add(new ListBoxItem{ Content = "Value to display"});

SubItem or ListItem??
Easy:

myListbox.Items.Add(object);

You can add any type of object into that collection or a specific ListBoxItem object like this one:

this.List.Items.Add(new ListBoxItem{ Content = "Value to display"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文