Skip to content

Instantly share code, notes, and snippets.

@cwensley
Last active August 29, 2015 14:26
Show Gist options
  • Save cwensley/fef45c2391362968cff7 to your computer and use it in GitHub Desktop.
Save cwensley/fef45c2391362968cff7 to your computer and use it in GitHub Desktop.
Command binding in Eto.Forms
using Eto.Forms;
using Eto.Drawing;
namespace MyEtoApp
{
public class MyModel
{
public MyModel()
{
MyCommand = new Command();
MyCommand.Executed += (sender, e) =>
{
var result = MessageBox.Show("Clicked! Do you want to disable the command?", buttons: MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
MyCommand.Enabled = false;
};
}
public Command MyCommand { get; } // can also be an ICommand
}
public class MainForm : Form
{
public MainForm()
{
ClientSize = new Size(200, 200);
// create menu item bound to command
var item = new ButtonMenuItem { Text = "Click Me!" };
item.BindDataContext(c => c.Command, (MyModel m) => m.MyCommand);
Menu = new MenuBar { Items = { new ButtonMenuItem { Text = "&File", Items = { item } } } };
// create toolbar item
var toolItem = new ButtonToolItem { Text = "Click Me!" };
toolItem.BindDataContext(c => c.Command, (MyModel m) => m.MyCommand);
ToolBar = new ToolBar { Items = { toolItem } };
// create button and bind to view model
var button = new Button { Text = "Click Me!" };
button.BindDataContext(c => c.Command, (MyModel m) => m.MyCommand);
Content = TableLayout.AutoSized(button, centered: true);
// set data context to view model
DataContext = new MyModel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment