Skip to content

Instantly share code, notes, and snippets.

@cwensley
Last active December 19, 2015 12:59
Show Gist options
  • Save cwensley/5958779 to your computer and use it in GitHub Desktop.
Save cwensley/5958779 to your computer and use it in GitHub Desktop.
Shows how to use a Windows Forms DateTimePicker in the wpf platform of Eto.Forms
using Eto.Forms;
using Eto.Platform.Wpf.Forms;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using swf = System.Windows.Forms;
namespace Eto.Test.Wpf
{
public class MyDateTimePickerHandler : WpfFrameworkElement<swf.Integration.WindowsFormsHost, DateTimePicker>, IDateTimePicker
{
swf.DateTimePicker picker;
public MyDateTimePickerHandler()
{
picker = new System.Windows.Forms.DateTimePicker();
picker.ShowCheckBox = true;
Mode = DateTimePicker.DefaultMode;
Value = null;
picker.ValueChanged += delegate
{
Widget.OnValueChanged(EventArgs.Empty);
};
Control = new swf.Integration.WindowsFormsHost();
Control.Child = picker;
}
public DateTimePickerMode Mode
{
get
{
switch (picker.Format)
{
case System.Windows.Forms.DateTimePickerFormat.Long:
return DateTimePickerMode.DateTime;
case System.Windows.Forms.DateTimePickerFormat.Short:
return DateTimePickerMode.Date;
case System.Windows.Forms.DateTimePickerFormat.Time:
return DateTimePickerMode.Time;
default:
throw new NotImplementedException();
}
}
set
{
switch (value)
{
case DateTimePickerMode.DateTime:
picker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
var format = CultureInfo.CurrentUICulture.DateTimeFormat;
picker.CustomFormat = format.ShortDatePattern + " " + format.LongTimePattern;
break;
case DateTimePickerMode.Date:
picker.Format = System.Windows.Forms.DateTimePickerFormat.Short;
break;
case DateTimePickerMode.Time:
picker.Format = System.Windows.Forms.DateTimePickerFormat.Time;
break;
default:
throw new NotImplementedException();
}
}
}
public DateTime MinDate
{
get { return picker.MinDate; }
set { picker.MinDate = value; }
}
public DateTime MaxDate
{
get { return picker.MaxDate; }
set { picker.MaxDate = value; }
}
public DateTime? Value
{
get
{
if (!picker.Checked) return null;
return picker.Value;
}
set
{
if (value != null)
{
picker.Value = value.Value;
picker.Checked = true;
}
else
picker.Checked = false;
}
}
public Drawing.Font Font
{
get; set;
}
public override Drawing.Color BackgroundColor
{
get; set;
}
}
}
class Startup
{
[STAThread]
static void Main (string[] args)
{
var generator = new Eto.Platform.Wpf.Generator ();
generator.Add<IDateTimePicker>(() => new MyDateTimePickerHandler());
var app = new TestApplication (generator);
app.Run (args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment