为什么不同的实例具有相同的依赖属性值?

 手机用户2602915671 发布于 2023-02-06 12:30

当我尝试在WPF/.NET 3.5中开发自定义控件时出现严重问题.

细节

1.当我在同一个窗口中添加两个实例时,看起来第二个将始终具有第一个依赖属性值,即使test2只是在不应用模板的情况下构造(例如:tes2.TopItems/CenterItems/BottomItems是相同的使用tes1包括Count,Items ...).

2.当我删除其中任何一个时,它会好的.

3. TopItems/CenterItems/BottomItems在OnApplyTemplate()中初始化.

4.更改属性"Calendar"和"CalendarViewType"将调用PropertyChangedCallBack().

5."TopItems","CenterItems","BttomItems"在Control的模板中的{TemplateBinding}中使用.

6.我尝试使用"Normal Property""{Binding RelativeSource = TemplatedParent}"而不是"Depdendency Property"和{TemplateBinding},它运作良好!!!!多奇怪!!!

请问,任何人都可以提供帮助吗?感谢looooooooooooooot!

类窗口


 
  
  
 

类CalendarTile:控件

    public static readonly DependencyProperty CalendarProperty = DependencyProperty.Register("Calendar", typeof(ICalendar), typeof(CalendarTitle), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, PropertyChangedCallback));
    public static readonly DependencyProperty CalendarViewTypeProperty = DependencyProperty.Register("CalendarViewType", typeof(CalendarViewType), typeof(CalendarTitle), new FrameworkPropertyMetadata(CalendarViewType.Week_Day, FrameworkPropertyMetadataOptions.AffectsRender, PropertyChangedCallback));
    public static readonly DependencyProperty TopItemsProperty = DependencyProperty.Register("TopItems", typeof(IEnumerable), typeof(CalendarTitle), new FrameworkPropertyMetadata(new ObservableCollection()));
    public static readonly DependencyProperty CenterItemsProperty = DependencyProperty.Register("CenterItems", typeof(IEnumerable), typeof(CalendarTitle), new FrameworkPropertyMetadata(new ObservableCollection()));
    public static readonly DependencyProperty BottomItemsProperty = DependencyProperty.Register("BottomItems", typeof(IEnumerable), typeof(CalendarTitle), new FrameworkPropertyMetadata(new ObservableCollection()));
    public IEnumerable TopItems
    {
        get { return (IEnumerable)GetValue(TopItemsProperty); }
        set { SetValue(TopItemsProperty, value); }
    }
    public IEnumerable CenterItems
    {
        get { return (IEnumerable)GetValue(CenterItemsProperty); }
        set { SetValue(CenterItemsProperty, value); }
    }
    public IEnumerable BottomItems
    {
        get { return (IEnumerable)GetValue(BottomItemsProperty); }
        set { SetValue(BottomItemsProperty, value); }
    }

PropertyCallBack()

 static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var calendarTile = (CalendarTitle)d;
        calendarTile.isLoaded = false;
        calendarTile.OnApplyTemplate();
    }

OnApplyTemplate()

 public override void OnApplyTemplate()
    {
        if (!isLoaded)
        {
            ClearItems();
            if (Calendar != null)
                PopulateItems();
            isLoaded = true;
        }
    }

PopulateItems()

 private void PopulateItems()
    {
        switch (CalendarViewType)
        {
            case CalendarViewType.Week_Day:
                {
                    foreach (var item in Calendar.Items)
                    {
                        if (item.Date.DayOfWeek == DayOfWeek.Sunday)
                            (TopItems as IList).Add(item);
                    }
                    CenterItems = BottomItems = Calendar.Items;
                    break;
                }

            case CalendarViewType.Month_Week:
                {
                    foreach (var item in Calendar.Items)
                    {
                        if (item.Date.DayOfYear == 1)
                            (TopItems as IList).Add(item);
                        if (item.Date.Day == 1)
                            (CenterItems as IList).Add(item);
                        if (item.Date.DayOfWeek == DayOfWeek.Monday)
                            (BottomItems as IList).Add(item);
                    }
                    break;
                }

            case CalendarViewType.Year_Month:
                {
                    foreach (var item in Calendar.Items)
                    {
                        if (item.Date.DayOfYear == 1)
                            (TopItems as IList).Add(item);
                        if (item.Date.Day == 1)
                            (CenterItems as IList).Add(item);
                        if (item.Date.DayOfWeek == DayOfWeek.Monday)
                            (BottomItems as IList).Add(item);
                    }
                    break;
                }
        }

    }

Thomas Leves.. 11

指定依赖项属性的默认值时,将在所有实例之间共享此默认值.因此,如果它是一个可变引用类型,就像集合一样,对该集合的任何更改都将反映在该类的所有实例中.

您应该保留默认值null,并在类构造函数中初始化集合.这样,该类的所有实例都将拥有自己的集合.

1 个回答
  • 指定依赖项属性的默认值时,将在所有实例之间共享此默认值.因此,如果它是一个可变引用类型,就像集合一样,对该集合的任何更改都将反映在该类的所有实例中.

    您应该保留默认值null,并在类构造函数中初始化集合.这样,该类的所有实例都将拥有自己的集合.

    2023-02-06 12:33 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有