数据源更改时总计未更改

时间:2019-07-23 02:18:07

标签: c# wpf data-binding inotifypropertychanged

我在项目中使用WPF。我想从Entities Framework Database获得总价值并在主窗口上显示它。它可以工作,但是当我从子窗口更改数据源上的值时,它不会自动工作。我想在子窗口上的数据更改后,主窗口上的总数将立即更改。

#namespace KhoLeco.ViewModel
{
public class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
}

// MainWindow:

namespace KhoLeco.ViewModel
{

public class MainViewModel : BaseViewModel
{
    private double _TienDauCon;
    public double TienDauCon { get => _TienDauCon; set { _TienDauCon = value; OnPropertyChanged(); } }
    public bool IsLoaded = false;       
    public MainViewModel()
    {           
        LoadedWindowCommand = new RelayCommand<Window>((p) => { return true; }, (p) =>
        {
            IsLoaded = true;
            if (p == null)
                return;
            p.Hide();
            LoginWindow loginWindow = new LoginWindow();
            loginWindow.ShowDialog();
            if (loginWindow.DataContext == null)
                return;
            var loginVM = loginWindow.DataContext as LoginViewModel;

            if (loginVM.IsLogin)
            {
                p.Show();                    
                LoadTienDau();                   
            }
            else
            {
                p.Close();
            }

        }
       );


 public void LoadTienDau()
    {

      TienDauCon =((int)DataProvider.Ins.DB.ChiTietPhieuDau.Sum(p => p.PayInfo) - (double)DataProvider.Ins.DB.ChiTietPhieuDau.Sum(p => p.Amount));
     }


}
}

//主窗口上的TextBlock:

<TextBlock Text="{Binding TienDauCon,IsAsync=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,StringFormat={}{0:#,#.00}}">
</TextBlock>

//子窗口:

namespace KhoLeco.ViewModel
{
public class TienDauViewModel : BaseViewModel
{

    private ObservableCollection<ChiTietPhieuDau> _List;
    public ObservableCollection<ChiTietPhieuDau> List { get => _List; set 
{ _List = value; OnPropertyChanged(); } }
    private ObservableCollection<XeTai> _XeTai;
    public ObservableCollection<XeTai> XeTai { get => _XeTai; set { _XeTai = value; OnPropertyChanged(); } }
    private ObservableCollection<MatHangDau> _MatHangDau;
    public ObservableCollection<MatHangDau> MatHangDau { get => _MatHangDau; set { _MatHangDau = value; OnPropertyChanged(); } }

    private ChiTietPhieuDau _SelectedItem;
    public ChiTietPhieuDau SelectedItem
    {
        get => _SelectedItem;
        set
        {
            _SelectedItem = value;
            OnPropertyChanged();
            if (SelectedItem != null)
            {
                Id = SelectedItem.Id;
                //DateSuply = SelectedItem.DateSuply;
                SelectedXeTai = SelectedItem.XeTai;
                SelectedMatHangDau = SelectedItem.MatHangDau;
                Count = SelectedItem.Count;
                Price = SelectedItem.Price;
                Amount = SelectedItem.Amount;
                DateBuy = SelectedItem.DateBuy;
                CountLocate = SelectedItem.CountLocate;
                MoreInfo = SelectedItem.MoreInfo;
                PayInfo = SelectedItem.PayInfo;                    
            }
        }
    }
    private XeTai _SelectedXeTai;
    public XeTai SelectedXeTai
    {
        get => _SelectedXeTai;
        set
        {
            _SelectedXeTai = value;
            OnPropertyChanged();
        }
    }
    private MatHangDau _SelectedMatHangDau;
    public MatHangDau SelectedMatHangDau
    {
        get => _SelectedMatHangDau;
        set
        {
            _SelectedMatHangDau = value;
            OnPropertyChanged();
        }
    }
    private int _Id;
    public int Id { get => _Id; set { _Id = value; OnPropertyChanged(); } }

    private int _Count;
    public int Count { get => _Count; set { _Count = value; OnPropertyChanged(); } }

    private Nullable<double> _Price;
    public Nullable<double> Price { get => _Price; set { _Price = value; OnPropertyChanged(); } }

    private Nullable<double> _Amount;
    public Nullable<double> Amount { get => _Amount; set { _Amount = value; OnPropertyChanged(); } }

    private Nullable<DateTime> _DateBuy;
    public Nullable<DateTime> DateBuy { get => _DateBuy; set { _DateBuy = value; OnPropertyChanged(); } }

    private Nullable<DateTime> _DateSuply;
    public Nullable<DateTime> DateSuply { get => _DateSuply; set { _DateSuply = value; OnPropertyChanged(); } }

    private Nullable<double> _CountLocate;
    public Nullable<double> CountLocate { get => _CountLocate; set { _CountLocate = value; OnPropertyChanged(); } }

    private Nullable<int> _PayInfo;
    public Nullable<int> PayInfo { get => _PayInfo; set { _PayInfo = value; OnPropertyChanged(); } }

    private string _MoreInfo;
    public string MoreInfo { get => _MoreInfo; set { _MoreInfo = value; OnPropertyChanged(); } }

    public ICommand PhieuDauCommand { get; set; }
    public ICommand EditCommand { get; set; }

    public TienDauViewModel()
    {
        List = new ObservableCollection<ChiTietPhieuDau>(DataProvider.Ins.DB.ChiTietPhieuDau.OrderByDescending(x => x.Id));
        XeTai = new ObservableCollection<XeTai>(DataProvider.Ins.DB.XeTai);
        MatHangDau = new ObservableCollection<MatHangDau>(DataProvider.Ins.DB.MatHangDau);

        PhieuDauCommand = new RelayCommand<object>((p) => { return true; }, (p) => 
        {
            PhieuDauWindow wd = new PhieuDauWindow();

            wd.ShowDialog();

        });


        EditCommand = new RelayCommand<object>((p) =>
        {
            if (SelectedItem == null)
                return false;
            var displayList = DataProvider.Ins.DB.ChiTietPhieuDau.Where(x => x.Id == SelectedItem.Id);
            if (displayList != null && displayList.Count() != 0)
                return true;
            return false;
        }, (p) =>
        {
            var TienDau = DataProvider.Ins.DB.ChiTietPhieuDau.Where(x => x.Id == SelectedItem.Id).SingleOrDefault();
            TienDau.Id = Id;
            TienDau.IdTruck = SelectedXeTai.Id;
            TienDau.IdObject = SelectedMatHangDau.Id;
            TienDau.Count = Count;
            TienDau.Price = Price;
            TienDau.Amount = Count * Price;
            TienDau.CountLocate = CountLocate;
            TienDau.DateBuy = DateBuy;                
            TienDau.PayInfo = PayInfo;
            TienDau.MoreInfo = MoreInfo;               
            DataProvider.Ins.DB.SaveChanges();

            // May I call LoadTienDau() here?
        });
    }


}
}

1 个答案:

答案 0 :(得分:0)

在创建对象时将调用构造函数,您可以添加一个“更改的事件”并将其放置在(LoadTienDau())或子窗口中的任何函数中,而不是构造函数或loadEvent