简体   繁体   English

如何以编程方式更新Listview(wpf)中特定行中的特定列

[英]How do i update a specific column in a specific row in Listview (wpf) programmatically

I have a listview table see the picture in the link 我有一个listview表, 请参阅链接中的图片

What i want to do is to change the column x in row y programmatically. 我想做的是以编程方式更改y行中的x列。 I am using wpf and a listview with the following xaml code. 我正在使用wpf和具有以下xaml代码的listview。

`<ListView x:Name="listView1" Height="153" Width="444">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header ="Code" Width="148"></GridViewColumn>
                        <GridViewColumn Header ="Name" Width="148"></GridViewColumn>
                        <GridViewColumn Header ="Country" Width="148"></GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>

` What i want to do is to change the column x in row y programmatically. `我想做的是以编程方式更改y行中的x列。 Something like this listview.Items[x].ColumnIndex[y] = "My value"; 像这样的listview.Items [x] .ColumnIndex [y] =“我的值”; I need to pass a string value and i am not using databinding there. 我需要传递一个字符串值,并且我不在那里使用数据绑定。

The picture and your XAML markup don't quite match. 图片和您的XAML标记不太匹配。 I highly recommend reading over the explanation of how ListViews work here: http://www.wpf-tutorial.com/listview-control/simple-listview/ 我强烈建议您在此处阅读有关ListViews如何工作的解释: http : //www.wpf-tutorial.com/listview-control/simple-listview/

When changing what shows in a ListView, one modifies the ItemsSource and its contents, not the visual "cell". 更改ListView中显示的内容时,将修改ItemsSource及其内容,而不是可视的“单元格”。 This is best done with data binding. 最好通过数据绑定来完成。

Let's say your ListView is setup like this: 假设您的ListView是这样设置的:

XAML: XAML:

<ListView x:Name="myListView">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Code}" Header="Code"/>
            <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"/>
            <GridViewColumn DisplayMemberBinding="{Binding Country}" Header="Country"/>
        </GridView>
    </ListView.View>
</ListView>

C#: C#:

public class MyInfo
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        ...

        ObservableCollection<MyInfo> items = new ObservableCollection<MyInfo>();
        items.Add(new MyInfo() { Code = "mycode1", Name = "myname1", Country = "mycountry1" });
        items.Add(new MyInfo() { Code = "mycode2", Name = "myname2", Country = "mycountry2" });
        items.Add(new MyInfo() { Code = "mycode3", Name = "myname3", Country = "mycountry3" });

        myListView.ItemsSource = items;
    }
}


To change the Name value in the second column, you would use: 要更改第二列中的“名称”值,可以使用:

items[1].Name = "mynewname2";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM