Home > Mobile >  WPF Observable Collection not updating
WPF Observable Collection not updating

Time:11-15

i'm quite the rookie in the WPF enviroment i have been scouring for a solution, although i'm sure it's just something very basic i have yet to understand

I'm trying to make use of Observable collection to update a Listview I have added a method in the viewmodel, i need to call from outside code to add another item to the list.

When i call method addTask in the ViewModel with debugger on, i can see it counts up 1 item in the list. But it doesn't add it to the ListView

Model:

    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

}

the ViewModel:

    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 });
       
    }

The list view i want to update (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>

CodePudding user response:

I have no idea how you've assigned viewmodel in app.xaml.

Just open the xaml file, which holds your listview and build your window as usual:

<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>   

As mentioned, replace _UserList with Tasks.

Your async TasksCreate() is creating new instance of TasksViewModel so it will never update current one.

PS: you can obtain viewmodel by:

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

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

    VM.TasksCreate();
}
  • Related