WPF Observable Collection 不更新

WPF Observable Collection not updating

我算是WPF环境的菜鸟了 我一直在寻找解决方案,尽管我确定这只是我尚未理解的非常基本的东西

我正在尝试使用 Observable 集合来更新 Listview 我在视图模型中添加了一个方法,我需要从外部代码调用以将另一个项目添加到列表中。

当我在调试器打开的情况下调用 ViewModel 中的方法 addTask 时,我可以看到它在列表中增加了 1 个项目。但它不会将其添加到 ListView

型号:

    public class Tasks : INotifyPropertyChanged
{
    private string taskName;
    private string fromTime;
    private string toTime;
    private string message;
    private string taskCreator;

    public string TaskName
    {
        get
        {
            return taskName;
        }
        set
        {
            taskName = value;
            OnPropertyChanged("TaskName");
        }
    }
    public string FromTime
    {
        get
        {
            return fromTime;
        }
        set
        {
            fromTime = value;
            OnPropertyChanged("FromTime");
        }
    }
    public string ToTime
    {
        get
        {
            return toTime;
        }
        set
        {
            toTime = value;
            OnPropertyChanged("ToTime");
        }
    }
    public string Message
    {
        get
        {
            return message;
        }
        set
        {
            message = value;
            OnPropertyChanged("Message");
        }
    }
    public string TaskCreator
    {
        get
        {
            return taskCreator;
        }
        set
        {
            taskCreator = value;
            OnPropertyChanged("TaskCreator");
        }
    }
   
    

    #region INotifyPropertyChanged Members  

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion

}

视图模型:

    class TasksViewModel 
{


    public TasksViewModel()
    {
        
            {
            _UsersList.Add(new Tasks() { TaskName = "TaskName1", FromTime = "03:00", ToTime = "07:00", TaskCreator = "TaskCreator1", Message = "Hello" });
            _UsersList.Add(new Tasks() { TaskName = "TaskName2", FromTime = "03:00", ToTime = "07:00", TaskCreator = "TaskCreator2", Message = "Hello" });
            _UsersList.Add(new Tasks() { TaskName = "TaskName3", FromTime = "03:00", ToTime = "07:00", TaskCreator = "TaskCreator3", Message = "Hello" });

            
        };
    }

    public ObservableCollection<Tasks> Tasks
    {
        get { return _UsersList; }
        
       
    }
    public ObservableCollection<Tasks> _UsersList { get; set; } = new ObservableCollection<Tasks>();


    public void addTask(string taskName, string fromTime, string toTime, string taskCreator, string message) 
    {
        _UsersList.Add(new Tasks() { TaskName = taskName, FromTime = fromTime, ToTime = toTime, TaskCreator = taskCreator, Message = message });
       
    }

我要更新的列表视图(Xaml)

  <ListView Name="TaskGrid1" Grid.Row="1" Grid.Column="1" Margin="4,4,12,13"  ItemsSource="{Binding Tasks}" RenderTransformOrigin="0.5,0.5" FontSize="30" >
            <ListView.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="0"/>
                    <TranslateTransform/>
                </TransformGroup>
            </ListView.RenderTransform>
            <ListView.View>
                <GridView x:Name="List00000600">
                    <GridViewColumn Header="Tid" DisplayMemberBinding="{Binding FromTime}"  Width="100"/>
                    <GridViewColumn Header="Opgave" DisplayMemberBinding="{Binding TaskName}"  Width="350" />
                    <GridViewColumn Header="Opretter" DisplayMemberBinding="{Binding TaskCreator}" Width="120" />
                    
                </GridView>
            </ListView.View>
        </ListView>

我不知道你是如何在 app.xaml 中分配视图模型的。

只需打开 xaml 文件,其中包含您的列表视图并像往常一样构建您的 window:

<Window
    ... (rest of xmlns) 
    xmlns:MyViewModels="clr-namespace:YourViewModelNamespace"
>
    <Window.DataContext>
        <MyViewModels:TasksViewModel/>
    </Window.DataContext>
    <Grid/Or any container>         
        ...
        <ListView... />
    </Grid/Or any container
</Window>   

如前所述,将 _UserList 替换为 Tasks。

您的异步 TasksCreate() 正在创建 TasksViewModel 的新实例,因此它永远不会更新当前实例。

PS: 可以通过以下方式获取viewmodel:

// this function belongs to mainwindow/anywindow
public void CodeBehindClickEvent(object sender, RoutedEventArgs e)
{
    var VM = (TasksViewModel)this.DataContext;

    VM.addTask("blabla", ...)

    VM.TasksCreate();
}