【问题标题】:Why do different instances have same dependency property value?为什么不同的实例具有相同的依赖属性值?
【发布时间】:2014-01-06 09:24:17
【问题描述】:

当我尝试在 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" 用于控件模板中的 {TemplateBinding}。

6.我尝试使用“Normal Property”“{Binding RelativeSource=TemplatedParent}”而不是“Depdendency Property”和{TemplateBinding},效果很好! !!!好诡异!!!

拜托,有人可以帮忙吗?非常感谢!

类窗口

<Window>
 <Grid>
  <common:CalendarTitle x:Name="test1" Calendar="{Binding Calendar}" CalendarViewType="Month_Week"></common:CalendarTitle>
  <common:CalendarTitle x:Name="test2" Calendar="{Binding Calendar}"></common:CalendarTitle>
 </Grid>
</Window>

类 CalendarTile:Control

    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<ICalendarItem>), typeof(CalendarTitle), new FrameworkPropertyMetadata(new ObservableCollection<ICalendarItem>()));
    public static readonly DependencyProperty CenterItemsProperty = DependencyProperty.Register("CenterItems", typeof(IEnumerable<ICalendarItem>), typeof(CalendarTitle), new FrameworkPropertyMetadata(new ObservableCollection<ICalendarItem>()));
    public static readonly DependencyProperty BottomItemsProperty = DependencyProperty.Register("BottomItems", typeof(IEnumerable<ICalendarItem>), typeof(CalendarTitle), new FrameworkPropertyMetadata(new ObservableCollection<ICalendarItem>()));
    public IEnumerable<ICalendarItem> TopItems
    {
        get { return (IEnumerable<ICalendarItem>)GetValue(TopItemsProperty); }
        set { SetValue(TopItemsProperty, value); }
    }
    public IEnumerable<ICalendarItem> CenterItems
    {
        get { return (IEnumerable<ICalendarItem>)GetValue(CenterItemsProperty); }
        set { SetValue(CenterItemsProperty, value); }
    }
    public IEnumerable<ICalendarItem> BottomItems
    {
        get { return (IEnumerable<ICalendarItem>)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;
        }
    }

填充项目()

 private void PopulateItems()
    {
        switch (CalendarViewType)
        {
            case CalendarViewType.Week_Day:
                {
                    foreach (var item in Calendar.Items)
                    {
                        if (item.Date.DayOfWeek == DayOfWeek.Sunday)
                            (TopItems as IList<ICalendarItem>).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<ICalendarItem>).Add(item);
                        if (item.Date.Day == 1)
                            (CenterItems as IList<ICalendarItem>).Add(item);
                        if (item.Date.DayOfWeek == DayOfWeek.Monday)
                            (BottomItems as IList<ICalendarItem>).Add(item);
                    }
                    break;
                }

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

    }

【问题讨论】:

  • 您的哪一个依赖属性在窗口的两个实例上似乎具有相同的值?
  • OnApplyTemplate() 不操纵任何依赖属性。 PopulateItems()的内容是什么? 究竟实际输出与您的预期有何不同?

标签: c# wpf dependency-properties


【解决方案1】:

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

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

【讨论】:

  • 谢谢!但情况可能有所不同。例如:加载test1时,test1.Top/Center/Bottom有1/8/37项。然后test2开始构建,我发现test2.Top/Center/Bottom也有1/8/37项!有什么建议吗?谢谢!
  • 谢谢你的朋友!你是完全正确的!!!!!!!你救了我的命和午餐!!!再次感谢!!
  • 纯正的小宝石!
  • 天啊!!!!!!!!!!!!对了!没想到用google这么快就找到答案了!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
相关资源
最近更新 更多