简体   繁体   中英

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.
  • 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.
  • 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.
  • 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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