如何以编程方式添加多列 ListViewItem 而无需任何数据绑定?

发布于 2024-09-08 05:22:02 字数 819 浏览 3 评论 0原文

我有一个带有 3 个标头的 ListView,在 XAML 中声明如下:

<ListView Name="myListView">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="H1"/>
      <GridViewColumn Header="H2"/>
      <GridViewColumn Header="H3"/>
    </GridView>
  </ListView.View>
</ListView>

我想以编程方式将 ListViewItem 添加到此 ListView,从而能够设置 ListViewItem 中分别位于第一列、第二列和第三列下的内容。到目前为止,我只得到了这一点:

ListViewItem l = new ListViewItem();
l.Content = "Content";
myListView.Items.Add(l);

这将每一列设置为字符串“Content”。如何更改上面的代码,以便添加一个 ListViewItem,该 ListViewItem 将分别在第一列、第二列和第三列下显示“内容 1”、“内容 2”和“内容 3”?我尝试在 ListViewItem 中查找 SubItem 或 ListViewSubItem 属性,但什么也没找到。

我认为有一个简单的解决方案,但也许我错了。请不要提及数据绑定,因为我只想回答以编程方式设置 Content 属性以反映每列中的单独更改的问题。

非常感谢。

I have a ListView with 3 headers, declared in XAML as follows:

<ListView Name="myListView">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="H1"/>
      <GridViewColumn Header="H2"/>
      <GridViewColumn Header="H3"/>
    </GridView>
  </ListView.View>
</ListView>

I want to programmatically add a ListViewItem to this ListView, being able to set the content within the ListViewItem that will go under the first, second, and third columns individually. So far, I have only gotten this far:

ListViewItem l = new ListViewItem();
l.Content = "Content";
myListView.Items.Add(l);

This sets each column to the string "Content". How can I change the code above so that I can add a ListViewItem that will display "Content 1", "Content 2", and "Content 3" under the first, second, and third columns respectively? I tried to look for a SubItem or ListViewSubItem property within ListViewItem, but found nothing.

I assume there's a simple solution, but maybe I'm wrong. Please do not mention data binding, because I only want an answer to the question of programmatically setting the Content property to reflect individual changes in each column.

Thank you very much.

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

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

发布评论

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

评论(2

南…巷孤猫 2024-09-15 05:22:02

这不是数据绑定。将 Binding 语句视为为列命名。

<ListView Name="myListView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="H1" DisplayMemberBinding="{Binding Col1}"/>
            <GridViewColumn Header="H2" DisplayMemberBinding="{Binding Col2}"/>
            <GridViewColumn Header="H3" DisplayMemberBinding="{Binding Col3}"/>
        </GridView>
    </ListView.View>
</ListView>

在代码中:

myListView.Items.Add(new { Col1 = "test1", Col2 = "Test2", Col3="test3"});

This is not databinding. Think of the Binding statement as giving the column a name.

<ListView Name="myListView">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="H1" DisplayMemberBinding="{Binding Col1}"/>
            <GridViewColumn Header="H2" DisplayMemberBinding="{Binding Col2}"/>
            <GridViewColumn Header="H3" DisplayMemberBinding="{Binding Col3}"/>
        </GridView>
    </ListView.View>
</ListView>

In code:

myListView.Items.Add(new { Col1 = "test1", Col2 = "Test2", Col3="test3"});
野の 2024-09-15 05:22:02

中,您将执行以下操作。

您必须先设置列标题,否则将不会显示任何内容。使用字符串数组添加列表视图项目。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        listView1.View=View.Details;
        //Set Columns
        listView1.Columns.Add("Col1");
        listView1.Columns.Add("Col2");
        listView1.Columns.Add("Col3");
        //Fill Rows
        ListViewItem lvi;
        lvi=new ListViewItem(new string[] { "A", "B", "C" });
        listView1.Items.Add(lvi);
        lvi=new ListViewItem(new string[] { "D", "E", "F" });
        listView1.Items.Add(lvi);
        lvi=new ListViewItem(new string[] { "G", "H", "I" });
        listView1.Items.Add(lvi);
    }
}

结果的屏幕截图为

Scr

In here is what you do.

You have to set the column headins first, otherwise nothing will show. The add the list view items using a string array.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        listView1.View=View.Details;
        //Set Columns
        listView1.Columns.Add("Col1");
        listView1.Columns.Add("Col2");
        listView1.Columns.Add("Col3");
        //Fill Rows
        ListViewItem lvi;
        lvi=new ListViewItem(new string[] { "A", "B", "C" });
        listView1.Items.Add(lvi);
        lvi=new ListViewItem(new string[] { "D", "E", "F" });
        listView1.Items.Add(lvi);
        lvi=new ListViewItem(new string[] { "G", "H", "I" });
        listView1.Items.Add(lvi);
    }
}

a screenshot of the result is

Scr

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