简体   繁体   中英

WPF MVVM Update Datagrid when Combobox value changes

I have a WPF application with a datagrid that is populated based on a month and year. Using the MVVM pattern, how do I update the datagrid when the user changes the month or year selected value?

View

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

You can do it in set methods

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

Be aware, that this will block UI thread, so if you are doing some long opperation like DB query make sure to use asynchronous call

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

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