编辑项目后如何刷新列表视图控件?

发布于 2024-12-03 21:51:36 字数 2758 浏览 1 评论 0原文

我开始使用 wpf listview 控件。我创建了一个“添加”按钮和一个“编辑”按钮。 “添加”按钮按预期工作 - 每当我将新项目添加到列表中时,它就会显示。 我的问题是“编辑”按钮 - 通知 listView 控件项目已更改的正确方法是什么? (它在附加代码中工作,我只是想知道是否有更好的方法)

这是我的代码:

Xaml:

<Window x:Class="WpfApplication5.MainWindow" Name="This"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    >
<Grid>
    <ListView   Name="Mylist"
                ItemsSource= "{Binding ElementName=This, Path=People}"
                SelectionMode="Single"
                >
        <ListView.View>
                <GridView AllowsColumnReorder="false">
                    <GridViewColumn 
                            Header="Name" 
                            Width="Auto" 
                            DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn 
                            Header="Id" 
                            Width="Auto" 
                            DisplayMemberBinding="{Binding Path=Id}" />
                </GridView>
            </ListView.View>
    </ListView>
    <StackPanel Orientation="Horizontal" Height="45" Margin="190,133,197,133">
        <Button 
                    Content="Add"
                    Click="AddButton_Click"
                    />
        <Button 
                    Content="Edit"
                    Click="EditButton_Click"
                    />
    </StackPanel>
</Grid>
</Window>

代码隐藏:

namespace WpfApplication5
{
public class PersonalDetails
{
    public string Name {get; set;}
    public string Id {get; set;}
}

public partial class MainWindow : Window
{
    private ObservableCollection<PersonalDetails> people = new ObservableCollection<PersonalDetails>();
    public ObservableCollection<PersonalDetails> People
    {
        get { return this.people; }
    }

    public MainWindow()
    {
        PersonalDetails p1 = new PersonalDetails();
        p1.Name = "Jeff";
        p1.Id = "111";
        people.Add(p1);
        InitializeComponent();
    }

    private void AddButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        PersonalDetails p2 = new PersonalDetails();
        p2.Name = "Tom";
        p2.Id = "222";
        people.Add(p2);
    }

    private void EditButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        PersonalDetails pItem = (PersonalDetails)Mylist.SelectedItem;
        if (pItem == null)
        {
            return;
        }
        pItem.Name = "Dan";
        Mylist.Items.Refresh();

    }

}
}

I started playing with the wpf listview control. I've created an "add" button and an "edit" button.
The "add" button works as expected - whenever I'm adding a new item to the list it gets displayed.
My problem is with the "edit" button - what is the correct way of informing the listView control that an item was changed? (it works in the attached code, i just wanted to know if there is a better way)

This is my code:

Xaml:

<Window x:Class="WpfApplication5.MainWindow" Name="This"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    >
<Grid>
    <ListView   Name="Mylist"
                ItemsSource= "{Binding ElementName=This, Path=People}"
                SelectionMode="Single"
                >
        <ListView.View>
                <GridView AllowsColumnReorder="false">
                    <GridViewColumn 
                            Header="Name" 
                            Width="Auto" 
                            DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn 
                            Header="Id" 
                            Width="Auto" 
                            DisplayMemberBinding="{Binding Path=Id}" />
                </GridView>
            </ListView.View>
    </ListView>
    <StackPanel Orientation="Horizontal" Height="45" Margin="190,133,197,133">
        <Button 
                    Content="Add"
                    Click="AddButton_Click"
                    />
        <Button 
                    Content="Edit"
                    Click="EditButton_Click"
                    />
    </StackPanel>
</Grid>
</Window>

Code Behind:

namespace WpfApplication5
{
public class PersonalDetails
{
    public string Name {get; set;}
    public string Id {get; set;}
}

public partial class MainWindow : Window
{
    private ObservableCollection<PersonalDetails> people = new ObservableCollection<PersonalDetails>();
    public ObservableCollection<PersonalDetails> People
    {
        get { return this.people; }
    }

    public MainWindow()
    {
        PersonalDetails p1 = new PersonalDetails();
        p1.Name = "Jeff";
        p1.Id = "111";
        people.Add(p1);
        InitializeComponent();
    }

    private void AddButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        PersonalDetails p2 = new PersonalDetails();
        p2.Name = "Tom";
        p2.Id = "222";
        people.Add(p2);
    }

    private void EditButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        PersonalDetails pItem = (PersonalDetails)Mylist.SelectedItem;
        if (pItem == null)
        {
            return;
        }
        pItem.Name = "Dan";
        Mylist.Items.Refresh();

    }

}
}

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

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

发布评论

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

评论(3

软糯酥胸 2024-12-10 21:51:36

您的 PersonalDetails 类应实现 INotifyPropertyChanged 接口。

然后,当 Name 属性更改并且引发 PropertyChanged 事件时,WPF 绑定基础结构将通过刷新演示文稿进行响应。

Your PersonalDetails class should implement the INotifyPropertyChanged interface.

Then when the Name property changes and you raise the PropertyChanged event the WPF binding infrastructure will respond by refreshing the presentation.

暗地喜欢 2024-12-10 21:51:36

XAML:

<ListView Name="listResult"  ItemsSource="{Binding ItemsCollection}"></ListView>

隐藏代码:

private ObservableCollection<object> itemsCollection = new ObservableCollection<object>();

public ObservableCollection<object> ItemsCollection
{
    get { return this.itemsCollection; }
}


private void UpdateSectionsList()
{
    List<object> tempList = ... //Put your element here

    // clear the list before and reload the object you want to display
    itemsCollection.Clear();

    if (tempList.Count > 0)
    {
        foreach (object item in tempList)
        {
            itemsCollection.Add(item);
        }
    }
 }

XAML:

<ListView Name="listResult"  ItemsSource="{Binding ItemsCollection}"></ListView>

Code Behind:

private ObservableCollection<object> itemsCollection = new ObservableCollection<object>();

public ObservableCollection<object> ItemsCollection
{
    get { return this.itemsCollection; }
}


private void UpdateSectionsList()
{
    List<object> tempList = ... //Put your element here

    // clear the list before and reload the object you want to display
    itemsCollection.Clear();

    if (tempList.Count > 0)
    {
        foreach (object item in tempList)
        {
            itemsCollection.Add(item);
        }
    }
 }
岁月蹉跎了容颜 2024-12-10 21:51:36

ListView.Items.Refresh();

帮助了我

ListView.Items.Refresh();

Helped me

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