2016-12-07 67 views
0

所以,我有幾乎相同的事情設置與文本框/列表框等​​,但它只是似乎並不與數據網格工作..DataGrid不更新時,項目屬性發生變化

所以,我有一個觀點包含dataGrid的索引。

我創建了一個IndexModel類如下:

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

然後在我的IndexView創建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); 
    } 

我創建的DataContext和XAML

ItemsSource="{Binding Path=Schedules, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 

綁定它我甚至創建了一個簡單的空白。

Schedule selectedItem = (Schedule) dataGrid.SelectedItem; 
selectedItem.Name = "Testing"; 

它更新的ObservableCollection但DataGrid不更新......

我通過計算器的所有問題的答案看上去等,但還是沒能解決我的問題..

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

回答

2

ObservableCollection提出了通知,當您添加或從中刪除的項目:它存在的理由,是落實INotifyCollectionChanged。它還會在其屬性屬性更改時引發通知(它也實現INotifyPropertyChanged,因此您可以綁定到obcoll.Count)。

你不是在做那些;實際上,ObservableCollection沒有可能的方式知道你已經做了什麼。它會如何發現?誰會告訴它?

爲了知道其中一件物品有屬性發生了變化,該物品將不得不實施INotifyPropertyChanged,並且該藏品必須處理通知。那麼,一旦這個項目做到了這一點,就沒有必要收集這個項目的通知了。

帶有更改通知的規則是通知由其個人狀態已更改的對象引發。如果成員或包含項目發生更改,則不涉及父/容器。由於上面給出的理由,如果它試圖去做就沒有任何意義。

因此:Schedule需要執行INotifyPropertyChanged並且在其Name屬性值更改時提升PropertyChanged。上述

的規則意味着,如果你有一個類,綁定會聯繫一下,屆時除非它是不可變的(如String,例如),它需要實現INotifyPropertyChanged

+0

啊..這是有道理的。我改變了INotifyProperty而不是INotifyCollectionChanged。我會用CollectionChanged試用它,並儘快給出更新 – Wkjjjj

+1

@Wkjjjj什麼?你在說什麼?!基本上沒有任何理由實現'INotifyCollectionChanged'。你是否在說'Schedule'實現了'INotifyPropertyChanged'?如果是這樣的話,那麼問題就不是我認爲的問題了。 –

+1

@Wkjjjj你可以分享'Schedule'的來源嗎? –

2

根據您的示例,如果在集合中動態添加或刪除計劃,應該起什麼作用。

但是,如果你想從UI更新一個給定的時間表,如:

selectedItem.Name = "Testing"; 

你需要的是更新Shedule項目本身,附表不是集合。

換句話說,如果您希望在視圖中對其進行編輯,則需要視圖模型作爲Schedule。你也需要爲Shedule提供一個數據模板,讓WPF知道它是如何渲染Shedule的。

希望它有幫助。

+0

所以我遵循你的建議,只是做了我的財產,並嘲笑其中的數據。然後我用這種方式編輯: Schedules.FirstOrDefault(x => x.ID == 1).Name ==「Test」; 我編輯綁定到ItemSource的項目(通過調試集合檢查100%更新) datagrid保持不變。 – Wkjjjj

+0

你的DataTemplate是什麼樣的? http://www.wpftutorial.net/datatemplates.html – Ouarzy

相關問題