2010-12-18 18:23:09 -04:00
|
|
|
using System;
|
2011-02-20 19:01:29 -04:00
|
|
|
using System.ComponentModel;
|
2010-12-18 18:23:09 -04:00
|
|
|
using System.Windows.Forms;
|
|
|
|
using ArducopterConfigurator.PresentationModels;
|
|
|
|
|
|
|
|
namespace ArducopterConfigurator.Views
|
|
|
|
{
|
|
|
|
// cannot be abstract due to vs2008 designer
|
2011-02-20 19:01:29 -04:00
|
|
|
public class ViewCommon<T> : UserControl, IView<T> where T : IPresentationModel, INotifyPropertyChanged
|
2010-12-18 18:23:09 -04:00
|
|
|
{
|
2011-02-13 05:25:54 -04:00
|
|
|
public virtual void SetDataContext(T vm)
|
2010-12-18 18:23:09 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-02-20 19:01:29 -04:00
|
|
|
protected void BindButtons(T vm)
|
2010-12-18 18:23:09 -04:00
|
|
|
{
|
|
|
|
foreach (var c in this.Controls)
|
|
|
|
if (c is Button)
|
|
|
|
(c as Button).Click += btn_Click;
|
2011-02-20 19:01:29 -04:00
|
|
|
|
|
|
|
vm.PropertyChanged += CheckCommandStates;
|
2010-12-18 18:23:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void btn_Click(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
var cmd = (sender as Button).Tag as ICommand;
|
|
|
|
|
|
|
|
if (cmd != null)
|
|
|
|
if (cmd.CanExecute(null))
|
|
|
|
cmd.Execute(null);
|
|
|
|
}
|
|
|
|
|
2011-02-20 19:01:29 -04:00
|
|
|
void CheckCommandStates(object sender, PropertyChangedEventArgs e)
|
|
|
|
{
|
|
|
|
foreach (var c in this.Controls)
|
|
|
|
{
|
|
|
|
var btn = c as Button;
|
|
|
|
if (btn == null) continue;
|
|
|
|
var cmd = btn.Tag as ICommand;
|
|
|
|
if (cmd != null)
|
|
|
|
btn.Enabled = cmd.CanExecute(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
public Control Control
|
|
|
|
{
|
|
|
|
get { return this; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|