简体   繁体   中英

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

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.

ObservableCollection raises notifications when you add or remove items from it: Its reason for existing is to implement INotifyCollectionChanged . It also raises notifications when its own properties change (it implements INotifyPropertyChanged as well, so you can bind to obcoll.Count ).

You're not doing either of those; in fact, there is no possible way for the ObservableCollection to know you've done anything at all. How would it find out? Who would be telling it?

For the collection to know that one of its items has had a property changed, the item would have to implement INotifyPropertyChanged and the collection would have to handle the notifications. Well, once the item does that, there's no need for the collection to get involved in the item's notifications at all.

The rule with change notifications is that a notification is raised by the object whose own personal state has changed. If a member or contained item changes, the parent/container is not involved. For reasons given above, it wouldn't make any sense if it tried to.

So: Schedule needs to implement INotifyPropertyChanged and raise PropertyChanged when its Name property value changes.

The rule above implies that if you have a class that bindings are going to touch, then unless it's immutable (like String , for example), it needs to implement INotifyPropertyChanged .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM