2014-02-05 64 views
0

我有一个基于一个月和一年填充的数据网格的WPF应用程序。使用MVVM模式,当用户更改月份或年份选定值时,如何更新数据网格?当Combobox值发生变化时WPF MVVM更新Datagrid

查看

<ComboBox DockPanel.Dock="Left" Name="comboBoxMonth" ItemsSource="{Binding Months}" SelectedItem="{Binding SelectedMonth}" TabIndex="0"></ComboBox> 
<ComboBox DockPanel.Dock="Left" Name="comboBoxYear" ItemsSource="{Binding Years}" SelectedItem="{Binding SelectedYear}" TabIndex="0"></ComboBox> 

<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding BudgetEntries}"></DataGrid> 

视图模型

public class BudgetEntryViewModel : INotifyPropertyChanged 
{ 
    private FinancialManagement4MEContext context = new FinancialManagement4MEContext(); 
    private int _SelectedMonth = GetDefaultMonth(); 
    private ObservableCollection<int> _Years; 
    private int _SelectedYear = GetDefaultYear(); 
    private ObservableCollection<BudgetEntry> _BudgetEntries; 
    private static int GetDefaultMonth() 
    { 
     int _monthnumber = DateTime.Now.Month; 

     if (_monthnumber == 1) { _monthnumber = 12; } 
     else { _monthnumber--; } 

     return _monthnumber; 
    } 

    private static int GetDefaultYear() 
    { 
     int _year = DateTime.Now.Year; 

     if (DateTime.Now.Month == 1) { _year--; } 

     return _year; 
    } 

    public BudgetEntryViewModel() 
    { 
     _BudgetEntries = new ObservableCollection<BudgetEntry>((from b in context.BudgetEntries 
                   where b.Year == _SelectedYear & b.Month == _SelectedMonth 
                   select b).ToList()); 
    } 

    public ObservableCollection<int> Months 
    { 
     get 
     { 
      ObservableCollection<int> _months = new ObservableCollection<int>(); 

      for (int i = 1; i <= 12; i++) 
      { 
       _months.Add(i); 
      } 

      return _months; 
     } 
    } 

    public int SelectedMonth 
    { 
     get 
     { 
      return _SelectedMonth; 
     } 
     set 
     { 
      if (_SelectedMonth != value) 
      { 
       _SelectedMonth = value; 
       RaisePropertyChanged("SelectedMonth"); 
      } 
     } 
    } 

    public ObservableCollection<int> Years 
    { 
     get 
     { 
      _Years = new ObservableCollection<int>(((from b in context.BudgetEntries 
                select b.Year).ToList<int>().Distinct()).ToList()); 

      if (DateTime.Now.Month == 2 && DateTime.Now.Year > _Years.Max()) 
      { 
       _Years.Add(DateTime.Now.Year); 
      } 

      return _Years; 
     } 
    } 

    public int SelectedYear 
    { 
     get 
     { 
      return _SelectedYear; 
     } 
     set 
     { 
      if (_SelectedYear != value) 
      { 
       _SelectedYear = value; 
       RaisePropertyChanged("SelectedYear"); 
      } 
     } 
    } 

    public ObservableCollection<BudgetEntry> BudgetEntries 
    { 
     get 
     { 
      return _BudgetEntries; 
     } 
     set 
     { 
      _BudgetEntries = value; 
     } 
    } 

    void RaisePropertyChanged(string prop) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

回答

1

你可以做到这一点的一套方法

public int SelectedMonth 
{ 
    get 
    { 
     return _SelectedMonth; 
    } 
    set 
    { 
     if (_SelectedMonth != value) 
     { 
      _SelectedMonth = value; 
      RaisePropertyChanged("SelectedMonth"); 
      UpdateData(); // This private method updates BudgetEntries collection 
     } 
    } 
} 

要知道,这将阻止UI线程,所以如果你正在做一些长时间的操作如DB查询请确保使用异步调用

例如Task

private void UpdateData() 
{ 
    IsBusy = true; 
    Task.Factory.CreateNew(()=>LongDBQuery()) 
       .ContinueWith(t => 
           { 
            if(t.Exception != null) 
            { 
             //Show Error Message 
             return; 
            } 
            BudgetEntries = t.Result; 
           } 
           , TaskScheduler.FromCurrentSynchronizationContext()); 
相关问题