'dataGrid doesn't update when item property has changed
So I have almost identical thing set up with TextBoxes/ListBoxes and so on but it just doesn't seem to work with dataGrid..
So I have a view Index which contains dataGrid.
I created a IndexModel class which is as follows:
public class IndexModel : INotifyPropertyChanged
{
private ObservableCollection<Schedule> _schedules;
public IndexModel(ObservableCollection<Schedule> schedules)
{
_schedules = schedules;
}
public ObservableCollection<Schedule> Schedules
{
get { return _schedules; }
set
{
_schedules = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Then in my IndexView I create IndexModel.
private ObservableCollection<Schedule> _schedules;
public Index(MainController controller)
{
_controller = controller;
InitializeComponent();
_schedules = controller.DatabaseController.GetSchedules() as ObservableCollection<Schedule>;
DataContext = new IndexModel(_schedules);
Log.Info($"UI Component {componentName} loaded succesfully",componentName, Source);
}
I create the DataContext and bind it in XAML
ItemsSource="{Binding Path=Schedules, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
I even created a simple void.
Schedule selectedItem = (Schedule) dataGrid.SelectedItem;
selectedItem.Name = "Testing";
And it updates the ObservableCollection yet dataGrid doesn't update...
I looked through all the answers of stackoverflow and such but still could not fix my problem..
public partial class Schedule
{
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public virtual User User { get; set; }
[StringLength(256)]
public string Comment { get; set; }
[StringLength(256)]
public string Description { get; set; }
[Required]
public Priorities Priority { get; set; }
public virtual UpdaterObject Object { get; set; }
public virtual ICollection<ScheduleAction> ScheduleAction { get; set; }
Solution 1:[1]
Schedule
needs to implement INotifyPropertyChanged
and raise PropertyChanged
when its Name
property value changes.
Solution 2:[2]
According to your example, what should work is if you add or delete a Schedule dynamically in the collection.
But if you want to update a given Schedule from the UI like:
selectedItem.Name = "Testing";
What you want is to update a Shedule item itself, not the collection of Schedule.
In other words you need a viewModel for the Schedule if you want it to be edited in your view. Also you need to provide a data Template for the Shedule to let WPF know how it is suppose to render a Shedule.
Hope it helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ouarzy |