列表框项目在 WPF 中没有刷新? [英] ListBox item doesn't get refresh in WPF?

查看:16
本文介绍了列表框项目在 WPF 中没有刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个项目的列表框.当双击每个项目时,用户可以选择编辑项目(项目文本).现在,一旦我更新了项目,我在列表框中的项目就不会更新.

I have a listbox which has couple of items. When double clicked on each item, the user get option to edit item (text of item). Now once i update the item, my item in listbox doesn't get updated.

第一个窗口(有列表框的窗口)在 MainWindow.xaml 文件中,第二个窗口在 EditTaskView.xaml(让我们编辑项目文本的那个)文件中.

The first window (one which has listbox) is in MainWindow.xaml file and second window is in EditTaskView.xaml(one which let's edit the items text) file.

在列表中显示项目的代码是:

The code that displays items in lists is:

Main.Windows.cs

Main.Windows.cs

公共静态 ObservableCollection 任务列表;

public static ObservableCollection TaskList;

    public void GetTask()
    {
        TaskList = new ObservableCollection<Task>
                           {
                               new Task("Task1"),
                               new Task("Task2"),
                               new Task("Task3"),
                               new Task("Task4")
                           };

        lstBxTask.ItemsSource = TaskList;
    }

private void lstBxTask_MouseDoubleClick(object sender, MouseButtonEventArgs e){

private void lstBxTask_MouseDoubleClick(object sender, MouseButtonEventArgs e) {

        var selectedTask = (Task)lstBxTask.SelectedItem;
        EditTask.txtBxEditedText.Text = selectedTask.Taskname;
        EditTask.PreviousTaskText = selectedTask.Taskname;  
        EditTask.Visibility = Visibility.Visible;
    } 

显示列表的xaml代码:

The xaml code that displays the list:

<ListBox x:Name="lstBxTask" Style="{StaticResource ListBoxItems}" MouseDoubleClick="lstBxTask_MouseDoubleClick">
        <ListBox.ItemTemplate>              
            <DataTemplate>                   
                <StackPanel>
                    <Rectangle Style="{StaticResource LineBetweenListBox}"/>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Taskname}"  Style="{StaticResource TextInListBox}"/>
                        <Button Name="btnDelete" Style="{StaticResource DeleteButton}" Click="btnDelete_Click"/>                                                     
                    </StackPanel>
                </StackPanel>                   
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox> 
    <ToDoTask:EditTaskView x:Name="EditTask" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Visibility="Collapsed"/> 

TaskEditView.xaml 中的保存按钮执行以下操作:

The Save button in TaskEditView.xaml does this:

    public string PreviousTaskText { get; set; }

 private void btnSaveEditedText_Click(object sender, RoutedEventArgs e)
 {
            foreach (var t in MainWindow.TaskList)
            {
                if (t.Taskname == PreviousTaskText)
                {
                    t.Taskname = txtBxEditedText.Text;
                }
           }

            Visibility = Visibility.Collapsed;

 }

TaskList 是 ObservableCollection,但我一旦更新了值,UI 就会刷新.但似乎不是这样.

TaskList is the ObservableCollection, and i though once you update the value the UI gets refreshed. But doesn't seem to work that way.

我错过了什么?

推荐答案

看起来您可能需要在 Task 类中实现 INotifyPropertyChanged.该界面提醒 UI 底层数据已更改,需要重新拉取数据以更新视图.

Looks like you may need to implement INotifyPropertyChanged in your Task class. This interface alerts the UI that underlying data has changed and it needs to repull the data to update the view.

这篇关于列表框项目在 WPF 中没有刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆