当Combobox值更改时,WPF MVVM更新Datagrid

时间:2014-02-05 16:06:04

标签: c# 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>

视图模型

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 个答案:

答案 0 :(得分:1)

你可以用set方法

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

请注意,这将阻止UI线程,因此如果您正在进行一些长期操作,例如数据库查询,请确保使用异步调用

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