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

发布于 2024-10-14 23:48:29 字数 1210 浏览 4 评论 0原文

我正在尝试使用数据绑定创建我的日历控件。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼睛不忧郁 2024-10-21 23:48:29

嗯...我们从哪里开始?我注意到以下几点:

  • 如果您使用依赖属性,则无需从 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:

  • 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 (e.g. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文