Skip to content

Instantly share code, notes, and snippets.

@cwensley
Created July 19, 2016 16:54
Show Gist options
  • Save cwensley/c211a778dcdba82c2ea243961c84ab7a to your computer and use it in GitHub Desktop.
Save cwensley/c211a778dcdba82c2ea243961c84ab7a to your computer and use it in GitHub Desktop.
Demo code binding to a view model, styles, and layout written during live session
using System;
using Eto.Forms;
using Eto.Drawing;
using System.Windows.Input;
using System.Runtime.CompilerServices;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace EtoApp2
{
public static class StyleID
{
public static class StackLayout
{
public const string Top = "Rhino.top";
}
}
public partial class MainForm
{
static MainForm()
{
//Eto.Style.Add<Control>(null, c => c.BackgroundColor = Colors.Red);
Eto.Style.Add<Label>(null, label =>
{
label.VerticalAlignment = VerticalAlignment.Center;
});
Eto.Style.Add<Label>(StyleID.StackLayout.Top, label =>
{
label.VerticalAlignment = VerticalAlignment.Top;
label.BackgroundColor = Colors.Blue;
});
}
public MainForm()
{
InitializeComponent();
}
}
enum Gender
{
Male,
Female,
X,
Unset
}
class MyDropDownItem
{
public string Name { get; set; }
}
class MyTreeItem : TreeGridItem
{
public Image Image { get; set; }
public string Name { get; set; }
}
class MyModel : INotifyPropertyChanged
{
Command saveCommand;
string firstName;
string lastName;
Gender gender;
bool useHomeAddress;
ObservableCollection<MyDropDownItem> collection;
TreeGridItemCollection treeCollection;
Image image;
public static Dictionary<Gender, string> GenderStrings = new Dictionary<Gender, string>
{
{ Gender.Male, "Male!" },
{ Gender.Unset, "Unset" },
{ Gender.Female, "Female!" },
{ Gender.X, "X!" }
};
public MyModel()
{
saveCommand = new Command((sender, e) =>
{
FirstName = "Something Else " + UseHomeAddress.ToString();
collection.Add(new MyDropDownItem { Name = "Item " + collection.Count });
});
collection = new ObservableCollection<MyDropDownItem>();
collection.Add(new MyDropDownItem { Name = "First Item" });
treeCollection = new TreeGridItemCollection();
treeCollection.Add(new MyTreeItem { Name = "First Node", Children = { } });
saveCommand.Enabled = false;
}
float logicalPixelSize;
public float LogicalPixelSize
{
get { return logicalPixelSize; }
set
{
if (logicalPixelSize != value)
{
logicalPixelSize = value;
OnPropertyChanged();
OnPropertyChanged(nameof(Image));
}
}
}
public Image Image
{
get { return image; }
}
Size imageSize;
public Size ImageSize
{
get { return imageSize; }
set
{
if (imageSize != value)
{
imageSize = value;
List<IconFrame> frames = new List<IconFrame>();
for (int i = 1; i <= 3; i++)
{
var bitmap = new Bitmap(value.Width * i, value.Height * i, PixelFormat.Format32bppRgba);
using (var g = new Graphics(bitmap))
{
g.ScaleTransform(i);
Color color = Colors.White;
if (i == 1) color = Colors.Red;
if (i == 2) color = Colors.Green;
if (i == 3) color = Colors.Blue;
g.DrawLine(color, PointF.Empty, new PointF(value.Width, value.Height));
g.DrawText(Fonts.Sans(20), Colors.Black, new PointF(100, 100), value.ToString());
}
frames.Add(new IconFrame(i, bitmap));
}
var icon = new Icon(frames);
image = icon;
OnPropertyChanged();
OnPropertyChanged(nameof(Image));
}
}
}
public TreeGridItemCollection MyTreeItems
{
get { return treeCollection; }
}
public Gender Gender
{
get { return gender; }
set
{
if (gender != value)
{
gender = value;
OnPropertyChanged();
}
}
}
public bool UseHomeAddress
{
get { return useHomeAddress; }
set
{
if (useHomeAddress != value)
{
useHomeAddress = value;
saveCommand.Enabled = value;
OnPropertyChanged();
}
}
}
public string FirstName
{
get { return firstName; }
set
{
if (firstName != value)
{
firstName = value;
OnPropertyChanged();
OnPropertyChanged("FullName");
}
}
}
public string FullName
{
get { return FirstName + " " + LastName; }
}
public string LastName
{
get { return lastName; }
set
{
if (lastName != value)
{
lastName = value;
OnPropertyChanged();
OnPropertyChanged("FullName");
}
}
}
public IList<MyDropDownItem> MyItems
{
get
{
return collection;
}
}
public ICommand SaveCommand
{
get
{
return saveCommand;
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
partial class MainForm : Form
{
void InitializeComponent()
{
Title = "My Eto Form";
//ClientSize = new Size(400, 350);
var spacing = 5;
var firstNameTextBox = new TextBox();
//firstNameTextBox.TextBinding.BindDataContext((MyModel m) => m.FirstName);
firstNameTextBox.BindDataContext(c => c.Text, (MyModel m) => m.FirstName);
var lastNameTextBox = new TextBox();
lastNameTextBox.TextBinding.BindDataContext((MyModel m) => m.LastName);
var fullNameLabel = new Label { };
fullNameLabel.TextBinding.BindDataContext((MyModel m) => m.FullName);
var saveButton = new Button { Text = "Save" };
saveButton.BindDataContext(c => c.Command, (MyModel m) => m.SaveCommand);
var genderDropDown = new EnumRadioButtonList<Gender>();
genderDropDown.Orientation = Orientation.Vertical;
genderDropDown.GetText = (gender) => MyModel.GenderStrings[gender];
genderDropDown.SortAlphabetically = true;
//genderDropDown.AddValue += (sender, e) => e.ShouldAdd = e.Value != Gender.X;
genderDropDown.BindDataContext(c => c.SelectedValue, (MyModel m) => m.Gender);
var dropDown = new DropDown();
dropDown.ItemTextBinding = Binding.Property((MyDropDownItem m) => m.Name);
dropDown.BindDataContext(c => c.DataStore, (MyModel m) => m.MyItems);
var checkBox = new CheckBox { Text = "Use Home Address" };
//checkBox.BindDataContext(c => c.Checked, (MyModel m) => m.UseHomeAddress);
checkBox.CheckedBinding.BindDataContext((MyModel m) => m.UseHomeAddress);
var radioButtonList = new RadioButtonList();
radioButtonList.Items.Add("Item 1");
var radioStack = new StackLayout { Orientation = Orientation.Horizontal };
RadioButton controller = null;
for (int i = 0; i < 10; i++)
{
var radio = new RadioButton(controller);
radio.Text = "Radio " + i;
//controller = controller ?? radio;
radioStack.Items.Add(radio);
}
var panel = new Panel();
var imageView = new ImageView();
panel.BindDataContext(c => c.Size, (MyModel m) => m.ImageSize);
imageView.BindDataContext(c => c.Image, (MyModel m) => m.Image);
panel.Content = imageView;
var gridView = new TreeGridView { Height = 300 };
gridView.Columns.Add(new GridColumn
{
HeaderText = "Some Text",
Editable = true,
DataCell = new ImageTextCell {
ImageBinding = Binding.Property((MyTreeItem m) => m.Image),
TextBinding = Binding.Property((MyTreeItem m) => m.Name)
}
});
gridView.Columns.Add(new GridColumn
{
HeaderText = "Some Text2",
DataCell = new ImageTextCell
{
ImageBinding = Binding.Property((MyTreeItem m) => m.Image),
TextBinding = Binding.Property((MyTreeItem m) => m.Name)
}
});
UpdateBindings(BindingUpdateMode.Destination);
var image = Icon.FromResource("EtoApp2.TestIcon.ico");
var treeCollection = new TreeGridItemCollection();
treeCollection.Add(new MyTreeItem {
Name = "First Node",
Image = image,
Expanded = true,
Children = {
new MyTreeItem { Name = "Child Item", Image = image }
}
});
gridView.DataStore = treeCollection;
//gridView.BindDataContext(c => c.DataStore, (MyModel m) => m.MyTreeItems);
var model = new MyModel { FirstName = "Curtis", Gender = Gender.Unset };
this.DataContext = model;
Content = new StackLayout
{
Padding = 10,
Spacing = spacing,
DataContext = model,
HorizontalContentAlignment = HorizontalAlignment.Stretch,
Items =
{
fullNameLabel,
new TableLayout {
Spacing = new Size(spacing, spacing),
Rows = {
new TableRow(new Label { Text = "First Name" }, firstNameTextBox),
new TableRow("Last Name", lastNameTextBox),
new TableRow(new Label { Text = "Gender", Style = StyleID.Top }, TableLayout.AutoSized(genderDropDown)),
new TableRow(new TableCell(), checkBox),
new TableRow(new TableCell(), dropDown),
new TableRow(new TableCell(), radioStack),
}
},
new StackLayoutItem(gridView, expand: true),
new StackLayoutItem(panel, expand: true),
new StackLayout
{
Orientation = Orientation.Horizontal,
Spacing = 5,
Items = {
null,
saveButton,
new Button { Text = "Cancel" }
}
}
// add more controls here
}
};
/*
var stack = new StackLayout();
stack.Spacing = 10;
stack.Items.Add(new Label { Text = "Hello!" });
*/
/*
StackLayout buttons;
if (Eto.EtoEnvironment.Platform.IsMac)
{
buttons = new StackLayout
{
Orientation = Orientation.Horizontal,
Spacing = 5,
Items = {
null,
new Button { Text = "Save" },
new Button { Text = "Cancel" }
}
};
}
else {
buttons = new StackLayout
{
Orientation = Orientation.Horizontal,
Spacing = 5,
Items = {
null,
new Button { Text = "Cancel" },
new Button { Text = "Apply" },
}
};
}*/
}
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
Application.Instance.AsyncInvoke(() => MinimumSize = Size);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment