仅在选中选项卡时运行查询WPF MVVM

时间:2017-10-01 06:37:41

标签: c# wpf xaml mvvm datagrid

我正在寻找一种方法是检查我的ViewModel是否选择了某个选项卡并运行一些查询。这是我的情景:

我有2个标签 - Tab1包含一个带有一些代码隐藏的数据网格,而Tab2只包含一些绑定到我的ViewModel的文本框。两个选项卡共享一个数据贴纸。当我从Tab1中的数据网格中选择一行时,日期会发生变化,从而触发绑定到Tab2中文本框的一堆查询,因为所有查询都使用所选月份作为参数。

这使得它真的无效并且数据网格行选择变得非常慢。我怎么能避免这个?设置我的ViewModel以检查Tab2是否被选中并且只运行查询的简单方法是什么?我想在TabControl中绑定selectedIndex,但我无法让它工作。我很陌生,我真的很感激一些帮助。

视图模型

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Location.Model;
using System.Windows.Controls;

namespace Location.ViewModel
{
    public class LocationViewModel: INotifyPropertyChanged
    {
        public LocationViewModel()
        {
            SetEfficiency();
        }

        private DateTime _mDate = DateTime.Now;

        public DateTime MDate
        {
            get { return _mDate; }
            set 
            {
                _mDate = value;
                OnPropertyChanged("MDate");
                SetEfficiency();
            }
        }

        decimal efficiency;

        public decimal Efficiency
        {
            get { return efficiency; }
            set
            {
                efficiency = value;
                OnPropertyChanged("Efficiency");
            }
        }

        DailyEntities db = new DailyEntities();

        private void SetEfficiency()
        {
            var month;
            int.TryParse(MDate.ToString("MM"), out month);
            Efficiency = Convert.ToDecimal(db.LocationKPI.Where(a => a.sMonth == month).Select(a => a.Efficiency).FirstOrDefault());
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName = null)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

MainWindow.xaml.cs

    public MainWindow()
    {
        InitializeComponent();

        DataContext = new LocationViewModel();

        string connectionString = "datasource=xx.xx.xxx.xxx;port=xxx;username=xxxxx;password=xxxxx";
        string sMonth = DateTime.Now.ToString("MM");
        string sYear = DateTime.Now.ToString("yyyy");

        MySqlConnection connection = new MySqlConnection(connectionString);

        MySqlCommand cmd = new MySqlCommand("Select * from MyTable where MONTH(Date) = @sMonth AND YEAR(Date) = @sYear", connection);

        try
        {
            connection.Open();
            cmd.Parameters.Add(new MySqlParameter("sMonth", sMonth));
            cmd.Parameters.Add(new MySqlParameter("sYear", sYear));

            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            dtGrid.DataContext = dt;
            connection.Close();

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }



    }

    private void vDatePick_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
    {

            string connectionString = "datasource=xx.xx.xxx.xxx;port=xxx;username=xxxxx;password=xxxxx";
            string sMonth = DateTime.Parse(vDatePick.Text).ToString("MM");
            string sYear = DateTime.Parse(vDatePick.Text).ToString("yyyy");
            string vDate = DateTime.Parse(vDatePick.Text).ToString("yyyy-MM-dd");

            MySqlConnection connection = new MySqlConnection(connectionString);
            MySqlCommand cmd = new MySqlCommand("Select * from MyTable where MONTH(Date) = @sMonth AND YEAR(Date) = @sYear", connection);


            try
            {

                connection.Open();
                cmd.Parameters.Add(new MySqlParameter("sMonth", sMonth));
                cmd.Parameters.Add(new MySqlParameter("sYear", sYear));
                DataTable dt = new DataTable();
                dt.Load(cmd.ExecuteReader());
                connection.Close();
                dtGrid.DataContext = dt;

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

    }


    private void dtGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {

        DataRowView row_selected = dtGrid.SelectedItem as DataRowView;
        if (row_selected == null) return;
        vDatePick.Text = row_selected["Date"].ToString();
        txtHours.Text = row_selected["Hours"].ToString();

    }

谢谢

1 个答案:

答案 0 :(得分:2)

  • 添加您的TabControl SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay}
  • 更改您的方法如下:

    private void vDatePick_SelectedDateChanged(object sender, SelectionChangedEventArgs e) 
    {
        if(SelectedTabIndex == 1)
        {
        //... your code 
       }
    }