Community Server

The platform that enables you to build rich, interactive communities
Welcome to Community Server Sign in | Join | Help
in Search

hakimin

Using Windows Forms DateTime picker in WPF

WPF is great piece of technology coming out from Redmond , it's kinda of really good when you're building nice presentation, that really shines. But for most enterprise developer, who needs to deal with data frequently, the lack of certain data aware control is a huge limiting factor, and we all know that, there's no DateTime data control in WPF. Kevin Moore's bag o tricks created a pure WPF dateTime picker but the feature far behind what's offered by Windows Forms. Fortunately we could use any Windows Forms control in WPF with WindowForsm Intergration(crossbow).

Basically we're going to create a UserControl, put a grid in and off we go

<UserControl x:Class="Bespoke.Forensic.CommonControl.WpfDateAndTimePicker" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:swf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
Name="root">

<Grid>
<
Grid.ColumnDefinitions>
<
ColumnDefinition Width="*"/>
<
ColumnDefinition Width="Auto"/>
</
Grid.ColumnDefinitions>

Grid>
<
Grid.ColumnDefinitions>
<
ColumnDefinition Width="*"/>
<
ColumnDefinition Width="Auto"/>
</
Grid.ColumnDefinitions>

<wfi:WindowsFormsHost Grid.Column="0" Grid.Row="0" Margin="0">
<
swf:DateTimePicker x:Name="wfdateTimePicker" Format="Custom" CustomFormat="dd/MM/yyyy hh:mm tt"/>
</
wfi:WindowsFormsHost>
</
Grid>
</
UserControl>

wfi:WindowsFormsHost Grid.Column="0" Grid.Row="0" Margin="0">
<
swf:DateTimePicker x:Name="wfdateTimePicker" Format="Custom" CustomFormat="dd/MM/yyyy hh:mm tt"/>
</
wfi:WindowsFormsHost>
</
Grid>
</
UserControl>

<UserControl x:Class="Bespoke.Forensic.CommonControl.WpfDateAndTimePicker" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:swf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
Name="root">

<Grid>
<
Grid.ColumnDefinitions>
<
ColumnDefinition Width="*"/>
<
ColumnDefinition Width="Auto"/>
</
Grid.ColumnDefinitions>

Grid>
<
Grid.ColumnDefinitions>
<
ColumnDefinition Width="*"/>
<
ColumnDefinition Width="Auto"/>
</
Grid.ColumnDefinitions>

<wfi:WindowsFormsHost Grid.Column="0" Grid.Row="0" Margin="0">
<
swf:DateTimePicker x:Name="wfdateTimePicker" Format="Custom" CustomFormat="dd/MM/yyyy hh:mm tt"/>
</
wfi:WindowsFormsHost>
</
Grid>
</
UserControl>

wfi:WindowsFormsHost Grid.Column="0" Grid.Row="0" Margin="0">
<
swf:DateTimePicker x:Name="wfdateTimePicker" Format="Custom" CustomFormat="dd/MM/yyyy hh:mm tt"/>
</
wfi:WindowsFormsHost>
</
Grid>
</
UserControl>

<Grid>
<
Grid.ColumnDefinitions>
<
ColumnDefinition Width="*"/>
<
ColumnDefinition Width="Auto"/>
</
Grid.ColumnDefinitions>

<wfi:WindowsFormsHost Grid.Column="0" Grid.Row="0" Margin="0">
<
swf:DateTimePicker x:Name="wfdateTimePicker" Format="Custom" CustomFormat="dd/MM/yyyy hh:mm tt"/>
</
wfi:WindowsFormsHost>
</
Grid>
</
UserControl>

wfi:WindowsFormsHost Grid.Column="0" Grid.Row="0" Margin="0">
<
swf:DateTimePicker x:Name="wfdateTimePicker" Format="Custom" CustomFormat="dd/MM/yyyy hh:mm tt"/>
</
wfi:WindowsFormsHost>
</
Grid>
</
UserControl>

Then we need to create  proxy property for the WF DatePicker to be able to do DataBinding, we're going to need DependencyProperty

public static readonly DependencyProperty ValueProperty;

public static readonly DependencyProperty ValueProperty;

static WpfDateAndTimePicker()

{

IsDropDownOpenProperty = DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(WpfDateAndTimePicker),

new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDropDownOpenChanged, null));

ValueProperty = DependencyProperty.Register("Value", typeof(DateTime?), typeof(WpfDateAndTimePicker),

new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, null));

}

static WpfDateAndTimePicker()

{

IsDropDownOpenProperty = DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(WpfDateAndTimePicker),

new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDropDownOpenChanged, null));

ValueProperty = DependencyProperty.Register("Value", typeof(DateTime?), typeof(WpfDateAndTimePicker),

new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, null));

}

= DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(WpfDateAndTimePicker),

new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDropDownOpenChanged, null));

ValueProperty = DependencyProperty.Register("Value", typeof(DateTime?), typeof(WpfDateAndTimePicker),

new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, null));

}

new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDropDownOpenChanged, null));

ValueProperty = DependencyProperty.Register("Value", typeof(DateTime?), typeof(WpfDateAndTimePicker),

new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, null));

}

= DependencyProperty.Register("Value", typeof(DateTime?), typeof(WpfDateAndTimePicker),

new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, null));

}

new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, null));

}

Published Sunday, April 22, 2007 11:14 PM by erymuzuan
Filed under:

Comments

 

amir said:

1337 & c001

May 10, 2007 11:52 AM
 

amir said:

1337 & c001

May 10, 2007 11:53 AM
Anonymous comments are disabled
Powered by Community Server, by Telligent Systems