You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.4 KiB
50 lines
1.4 KiB
14 years ago
|
using System;
|
||
14 years ago
|
using System.ComponentModel;
|
||
14 years ago
|
using System.Windows.Forms;
|
||
|
using ArducopterConfigurator.PresentationModels;
|
||
|
|
||
|
namespace ArducopterConfigurator.Views
|
||
|
{
|
||
|
// cannot be abstract due to vs2008 designer
|
||
14 years ago
|
public class ViewCommon<T> : UserControl, IView<T> where T : IPresentationModel, INotifyPropertyChanged
|
||
14 years ago
|
{
|
||
14 years ago
|
public virtual void SetDataContext(T vm)
|
||
14 years ago
|
{
|
||
|
}
|
||
|
|
||
14 years ago
|
protected void BindButtons(T vm)
|
||
14 years ago
|
{
|
||
|
foreach (var c in this.Controls)
|
||
|
if (c is Button)
|
||
|
(c as Button).Click += btn_Click;
|
||
14 years ago
|
|
||
|
vm.PropertyChanged += CheckCommandStates;
|
||
14 years ago
|
}
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
|
||
14 years ago
|
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);
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
public Control Control
|
||
|
{
|
||
|
get { return this; }
|
||
|
}
|
||
|
}
|
||
|
}
|