<wfi:WindowsFormsHost><wf:DateTimePicker x:Name=”Dg” > </wf:DateTimePicker ></wfi:WindowsFormsHost>怎么样将选中的日期时间绑定到数据begin,
解决方案
10
DateTimePicker 不是DependencyObject类型,无法使用绑定模式,只能后台赋值。
25
最好的办法是用第三方的WPF的DateTimePicker,例如Extended Wpf ToolKit里就有。
假如要一定要绑定Winform的DateTimePicker,就不好用通用的WindowsFormsHost,而要本人添加一个依赖属性。
假如要一定要绑定Winform的DateTimePicker,就不好用通用的WindowsFormsHost,而要本人添加一个依赖属性。
using System; using System.Windows; using System.Windows.Forms; using System.Windows.Forms.Integration; namespace WpfApplication3 { class WinformDateTimePicker : WindowsFormsHost { public DateTime MyDateTime { get { return (DateTime)GetValue(MyDateTimeProperty); } set { SetValue(MyDateTimeProperty, value); } } public static readonly DependencyProperty MyDateTimeProperty = DependencyProperty.Register( "MyDateTime", typeof(DateTime), typeof(WinformDateTimePicker), new PropertyMetadata(OnMyDateTimeChanged)); static void OnMyDateTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { ((d as WinformDateTimePicker).Child as DateTimePicker).Value = (DateTime)e.NewValue; } catch { } } public WinformDateTimePicker() { var dateTimePicker = new DateTimePicker(); this.Child = dateTimePicker; this.MyDateTime = dateTimePicker.Value; dateTimePicker.ValueChanged += delegate { this.MyDateTime = dateTimePicker.Value; }; } } }
<Window x:Class=...> <StackPanel> <local:WinformDateTimePicker x:Name="datetimePicker" Height="200" /> <TextBlock Margin="0 30 0 0" Text="{Binding ElementName=datetimePicker, Path=MyDateTime}" Background="PeachPuff" /> </StackPanel> </Window>
5
你可能进入了一个误区,就是使用了wpf之后啥都想绑定。其实没这个必要,当然,假如是想学习怎么样实现又另当别论了。