Skip to content

Instantly share code, notes, and snippets.

@sbaer
Last active June 24, 2016 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbaer/b2ff6de6eda8b6d0a814 to your computer and use it in GitHub Desktop.
Save sbaer/b2ff6de6eda8b6d0a814 to your computer and use it in GitHub Desktop.
Sample use of Eto and Rhino
using System;
using Eto.Forms;
using Eto.Drawing;
using Eto;
using System.Collections.Generic;
namespace EtoRhinoTests
{
public class EtoRhinoCommand : Rhino.Commands.Command
{
public override string EnglishName { get { return "TestEtoRhino"; } }
public EtoRhinoCommand()
{
// The following code would typically be run at start from the hosting
// code (RhinoCommon, RhinoWindows, or RhinoMac)
new Application().Attach();
// Make things look a little different on each platform
if (Platform.Instance.IsMac)
{
Style.Add<Label>("heading", label => label.HorizontalAlign = HorizontalAlign.Right);
Style.Add<Button>("ok", b => b.Text = "Apply");
Style.Add<Button>("cancel", b => b.Text = "Cancel");
}
else
{
Style.Add<Button>("ok", b => b.Text = "OK");
Style.Add<Button>("cancel", b => b.Text = "Cancel");
}
}
// Simple container of state information. Controls can be bound
// to this class
public class MyClass
{
string myProperty;
public string MyProperty
{
get { return myProperty; }
set
{
myProperty = value;
Rhino.RhinoApp.WriteLine("Set property to {0}", value);
}
}
public int SliderValue { get; set; }
public string TextAreaValue { get; set; }
public bool CheckValue { get; set; }
public PointF EndPoint { get; set; }
}
protected override Rhino.Commands.Result RunCommand(Rhino.RhinoDoc doc, Rhino.Commands.RunMode mode)
{
// Create a sample dialog with several bound controls and
// show it modal to Rhino
var textBox = new TextBox();
textBox.TextBinding.BindDataContext<MyClass>(inst => inst.MyProperty, (inst, val) => inst.MyProperty = val);
//tb.BindDataContext<MyClass>(new DelegateBinding<TextBox, bool> (tbb => tbb.Visible), new DelegateBinding<MyClass, bool> (inst => inst.IsVisible), DualBindingMode.TwoWay);
var textArea = new TextArea();
textArea.TextBinding.BindDataContext<MyClass>(inst => inst.TextAreaValue, (inst, val) => inst.TextAreaValue = val);
var checkBox = new CheckBox();
checkBox.CheckedBinding.BindDataContext<MyClass>(inst => inst.CheckValue, (inst, val) => inst.CheckValue = val ?? false);
var slider = new Slider { TickFrequency = 25, MaxValue = 100 };
// note: need to add slider.ValueBinding
var binding = new ObjectBinding<Slider, int>(slider, c => c.Value, (c, v) => c.Value = v, (c, h) => c.ValueChanged += h, (c, h) => c.ValueChanged -= h);
binding.BindDataContext<MyClass>(inst => inst.SliderValue, (inst, val) => inst.SliderValue = val);
Dialog dlg = null;
bool isDown = false;
var drawable = new Drawable { Size = new Size(200, 200) };
drawable.Paint += (sender, e) =>
{
var myClass = drawable.DataContext as MyClass;
if (myClass != null)
e.Graphics.DrawLine(Colors.Blue, PointF.Empty, myClass.EndPoint);
};
drawable.MouseDown += (sender, e) =>
{
isDown = true;
e.Handled = true;
};
drawable.MouseUp += (sender, e) =>
{
isDown = false;
e.Handled = true;
};
drawable.MouseMove += (sender, e) =>
{
if (isDown)
{
var myClass = drawable.DataContext as MyClass;
if (myClass != null)
{
myClass.EndPoint = e.Location;
drawable.Invalidate();
e.Handled = true;
}
}
};
int recordCount = 0;
Action updateRecord = () =>
{
var rand = new Random();
dlg.DataContext = new MyClass
{
MyProperty = "Record " + recordCount,
SliderValue = rand.Next(0, 100),
TextAreaValue = "Text " + recordCount,
CheckValue = rand.Next(2) == 0,
EndPoint = new PointF(rand.Next(drawable.Size.Width), rand.Next(drawable.Size.Height))
};
recordCount++;
drawable.Invalidate();
};
var okButton = new Button { Style = "ok" };
okButton.Click += (sender, e) => dlg.Close();
var cancelButton = new Button { Style = "cancel" };
cancelButton.Click += (sender, e) => dlg.Close();
var fahrvegnugenButton = new Button { Text = "fahrvergnügen!!!!!!" };
fahrvegnugenButton.Click += (sender, e) => updateRecord();
IEnumerable<TableCell> buttons;
if (Platform.Instance.IsMac)
buttons = new TableCell[] { null, fahrvegnugenButton, cancelButton, okButton };
else
buttons = new TableCell[] { null, fahrvegnugenButton, okButton, cancelButton };
dlg = new Dialog
{
Title = "My Dialog",
DefaultButton = okButton,
AbortButton = cancelButton,
Resizable = true,
Content = new TableLayout(
new TableRow(new TableLayout(
new TableRow(new Label { Text = "Text Box", Style = "heading" }, textBox),
new TableRow(new Label { Text = "Text Area", Style = "heading" }, textArea),
new TableRow(new Label { Text = "Slider", Style = "heading" }, slider),
new TableRow(new Label { Text = "Check Box", Style = "heading" }, checkBox),
new TableRow(new Label { Text = "Drawable", Style = "heading" }, drawable) { ScaleHeight = true }
)) { ScaleHeight = true },
new TableLayout(new TableRow(buttons))
)
};
updateRecord();
dlg.ShowModal();
return Rhino.Commands.Result.Success;
}
}
}
@willi-mueller
Copy link

willi-mueller commented Jun 24, 2016

Compiling this example gives a type error in line 74:

Error CS1929: Type Eto.Forms.ObjectBinding<Eto.Forms.Slider,int>' does not contain a memberBindDataContext' and the best extension method overload Eto.Forms.BindableExtensions.BindDataContext<MyProject.EtoRhinoCommand.MyClass>(this Eto.Forms.IBindable, string, string, Eto.Forms.DualBindingMode, MyProject.EtoRhinoCommand.MyClass,MyProject.EtoRhinoCommand.MyClass)' requires an instance of typeEto.Forms.IBindable' (CS1929) (MyProject)

How can I fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment