简体   繁体   English

Silverlight自定义控件和数据绑定在WP7中无法正常工作

[英]Silverlight Custom Control and Databinding doesn't work properly in WP7

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! 感谢您的支持!

Hmm ... where do we start? 嗯...我们从哪里开始? Here's a few things I've noticed: 这是我注意到的几件事:

  • If you're using a dependency property, there's no need to call OnPropertyChanged from the Date property setter. 如果您使用的是依赖项属性,则无需从Date属性设置器中调用OnPropertyChanged
  • The dependency property declares the type as DateTime , but your public exposed property is of type object , which then requires you to cast it elsewhere. 依赖项属性将类型声明为DateTime ,但是您的公开公开属性是object类型,然后要求您将其转换为其他位置。
  • If Calendar_Loaded is to be called in more situations than in response to the Loaded event (such as the GotFocus event, then I'd recommend that you call it something else, or create a method with a relevant name (eg UpdateDateParts) and call it from properly named separate event handlers. 如果要在更多情况下调用Calendar_Loaded而不是响应Loaded事件(例如GotFocus事件,那么我建议您将其称为其他内容,或者创建具有相关名称的方法(例如UpdateDateParts)并调用它来自正确命名的单独事件处理程序。
  • Using fixed format specifiers when processing date strings does not localize well. 处理日期字符串时使用固定格式说明符不能很好地进行本地化。

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 the Text property of some text blocks/boxes in event handlers. 一些人,我建议你可以通过使用绑定和暴露Date依赖项属性的日期部分而不是手动更新某些的Text属性来以支持数据绑定(和重新模板化)的方式实现用户界面。事件处理程序中的文本块/框。 In fact, if you derive from Control instead of UserControl 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. 实际上,如果您从Control而不是UserControl派生,那么您可以创建并实现无外观控件,该控件具有由themes \\ generic.xaml中的样式定义的用户界面,该样式可由控件的用户重新定义。

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. 至于为什么在日历控件的不同实例中日期不正确,我们需要查看一些XAML /代码,以了解如何使用和初始化控件以便能够提供更好的答案。 However, I thought the above was worth putting in an Answer, instead of trying to say it in a Comment. 但是,我认为以上内容值得一提,而不是试图在评论中说出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM