WPF MVVM更新datagrid当Combobox值更改时



我有一个带有数据杂志的WPF应用程序,该应用程序是根据一个月和年份填充的。使用MVVM模式,当用户更改月份或年度值时,如何更新数据级?

查看

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

ViewModel

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

您可以在设置方法中进行

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

最新更新