Silverlight 自定义控件和数据绑定在 WP7 中无法正常工作
我正在尝试使用数据绑定创建我的日历控件。
public partial class Calendar : UserControl
{
public static readonly DependencyProperty DateProperty =
DependencyProperty.Register("Date", typeof(DateTime),
typeof(Calendar), null);
public object Date
{
get { return GetValue(DateProperty); }
set
{
SetValue(DateProperty, value);
OnPropertyChanged("Date");
}
}
public Calendar()
{
// Required to initialize variables
InitializeComponent();
DayText.Text = ((DateTime)Date).ToString("dd");
MonthText.Text = ((DateTime)Date).ToString("MMM");
this.Loaded += new RoutedEventHandler(Calendar_Loaded);
this.GotFocus += new RoutedEventHandler(Calendar_Loaded);
}
void Calendar_Loaded(object sender, RoutedEventArgs e)
{
DayText.Text = ((DateTime)Date).ToString("dd");
MonthText.Text = ((DateTime)Date).ToString("MMM");
}
}
但是当我使用此控件创建列表框时,相同的日历有错误的日期。我确信通过彻底数据绑定的日期是正确的,但我不明白为什么同一日历显示不同的日期(我注意到这是前一个日历控件实例的日期)
感谢您的支持!
I'm trying to create my calendar control with databinding.
public partial class Calendar : UserControl
{
public static readonly DependencyProperty DateProperty =
DependencyProperty.Register("Date", typeof(DateTime),
typeof(Calendar), null);
public object Date
{
get { return GetValue(DateProperty); }
set
{
SetValue(DateProperty, value);
OnPropertyChanged("Date");
}
}
public Calendar()
{
// Required to initialize variables
InitializeComponent();
DayText.Text = ((DateTime)Date).ToString("dd");
MonthText.Text = ((DateTime)Date).ToString("MMM");
this.Loaded += new RoutedEventHandler(Calendar_Loaded);
this.GotFocus += new RoutedEventHandler(Calendar_Loaded);
}
void Calendar_Loaded(object sender, RoutedEventArgs e)
{
DayText.Text = ((DateTime)Date).ToString("dd");
MonthText.Text = ((DateTime)Date).ToString("MMM");
}
}
But When I create the listbox with this control, same calndar have the wrong date. I'm sure that the Date passed thorough databinding is correct but I don't understand why same calender show a different day (I'm noticed that is the day of a previous calendar control intance)
Thank you for supporting!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯...我们从哪里开始?我注意到以下几点:
Date
属性设置器调用OnPropertyChanged
。DateTime
,但您的公开公开属性的类型为object
,这需要您将其强制转换到其他地方。Calendar_Loaded
而不是响应Loaded
事件(例如GotFocus
事件),那么我建议您可以将其称为其他名称,或者创建一个具有相关名称的方法(例如 UpdateDateParts),并从正确命名的单独事件处理程序中调用它,我建议 使用固定格式说明符。您可以通过使用绑定并公开
Date
依赖项属性的日期部分,以支持数据绑定(和重新模板化)的方式实现用户界面,而不是手动更新TextControl
而不是UserControl
派生,那么您可以创建并实际拥有其用户的外观控件。由 theme\generic.xaml 中的样式定义的接口,可以由控件的用户重新定义。至于为什么日期在日历控件的不同实例中不正确,我们需要查看您的一些 XAML/代码。查看控件的使用和初始化方式,以便能够提供更好的答案。然而,我认为上述内容值得放在一个答案中,而不是试图在评论中说出来。
Hmm ... where do we start? Here's a few things I've noticed:
OnPropertyChanged
from theDate
property setter.DateTime
, but your public exposed property is of typeobject
, which then requires you to cast it elsewhere.Calendar_Loaded
is to be called in more situations than in response to theLoaded
event (such as theGotFocus
event, then I'd recommend that you call it something else, or create a method with a relevant name (e.g. UpdateDateParts) and call it from properly named separate event handlers.In addition to that, I'd suggest that you could implement the user interface in a manner that supports databinding (and re-templating) by using bindings and exposing the date parts of the
Date
dependency property instead of manually updating theText
property of some text blocks/boxes in event handlers. In fact, if you derive fromControl
instead ofUserControl
then you can create and actuall lookless control that has it's user interface defined by a style in themes\generic.xaml that can be re-defined by users of your control.As for why the date is incorrect in different instances of your calendar control, we'd need to see some of your XAML/code to see how the control is being used and initialized to be able to provide a better answer. However, I thought the above was worth putting in an Answer, instead of trying to say it in a Comment.