Reputation: 585
I am looking for a way is to check in my ViewModel if a certain Tab is selected and run some queries. Here's my scenario:
I have 2 tabs - Tab1 contains a data grid with some code-behind and Tab2 only some textboxes binded to my ViewModel. Both tabs share a datapicker. When I select a row from the datagrid in Tab1 the date changes which fires a bunch of queries binded to the textboxes in Tab2 because all the queries use the selected month as a parameter.
This gives makes it really ineffective and the datagrid row selection becomes very slow. How can I avoid this? What's the easies way to set up my ViewModel to check if Tab2 is selected and only then run the queries? I was thinking of binding the selectedIndex in the TabControl but I couldn't get it to work. I am quite new at this and I would really appreciate some help.
ViewModel
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();
}
Thank you
Upvotes: 0
Views: 347
Reputation: 98
SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay}
Change your method like below:
private void vDatePick_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
if(SelectedTabIndex == 1)
{
//... your code
}
}
Upvotes: 2