Skip to content

Instantly share code, notes, and snippets.

@cwensley
Created March 2, 2017 21:09
Show Gist options
  • Save cwensley/a9d205848997445a7440d260a1261a4c to your computer and use it in GitHub Desktop.
Save cwensley/a9d205848997445a7440d260a1261a4c to your computer and use it in GitHub Desktop.
Shows how to create a helper method to set up binding for multiple properties of a check box.
using System;
using Eto.Forms;
using Eto.Drawing;
using System.ComponentModel;
using System.Windows.Input;
using System.Linq.Expressions;
namespace MyApp1
{
public class MyViewModel : INotifyPropertyChanged
{
int _prefix1;
int _prefix2 = 200;
bool _isChecked1;
bool _isChecked2 = true;
bool _isChecked3;
public int Prefix1
{
get { return _prefix1; }
set
{
if (_prefix1 != value)
{
_prefix1 = value;
OnPropertyChanged(nameof(Prefix1));
}
}
}
public bool IsChecked1
{
get { return _isChecked1; }
set
{
if (_isChecked1 != value)
{
_isChecked1 = value;
OnPropertyChanged(nameof(IsChecked1));
}
}
}
public int Prefix2
{
get { return _prefix2; }
set
{
if (_prefix2 != value)
{
_prefix2 = value;
OnPropertyChanged(nameof(Prefix2));
}
}
}
public bool IsChecked2
{
get { return _isChecked2; }
set
{
if (_isChecked2 != value)
{
_isChecked2 = value;
OnPropertyChanged(nameof(IsChecked2));
}
}
}
public bool IsChecked3
{
get { return _isChecked3; }
set
{
if (_isChecked3 != value)
{
_isChecked3 = value;
OnPropertyChanged(nameof(IsChecked3));
}
}
}
public ICommand DoSomething => new Command((sender, e) =>
{
Prefix1++;
Prefix2--;
IsChecked1 = !IsChecked1;
IsChecked2 = !IsChecked2;
IsChecked3 = !IsChecked3;
});
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public class MyPanel : Panel
{
CheckBox CreateCheck(string label, Expression<Func<MyViewModel, bool?>> checkedProperty, Expression<Func<MyViewModel, int>> prefixProperty = null)
{
var check = new CheckBox();
if (prefixProperty != null)
check.BindDataContext(c => c.Text, Binding.Property(prefixProperty).Convert(v => $"({v}) {label}"));
else
check.Text = label;
check.CheckedBinding.BindDataContext(checkedProperty);
return check;
}
public MyPanel()
{
var check1 = CreateCheck("Check box 1", m => m.IsChecked1, m => m.Prefix1);
var check2 = CreateCheck("Check box 2", m => m.IsChecked2, m => m.Prefix2);
var check3 = CreateCheck("Check box 3", m => m.IsChecked3);
var doSomethingButton = new Button { Text = "Do Something" };
doSomethingButton.BindDataContext(c => c.Command, (MyViewModel m) => m.DoSomething);
Content = new StackLayout
{
Items =
{
check1,
check2,
check3,
doSomethingButton
}
};
DataContext = new MyViewModel();
}
}
/// <summary>
/// Your application's main form
/// </summary>
public class MainForm : Form
{
public MainForm()
{
Title = "My Eto Form";
ClientSize = new Size(400, 350);
Content = new MyPanel();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment