Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么不同的实例具有相同的依赖属性值?;_C#_Wpf_Dependency Properties - Fatal编程技术网

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

C# 为什么不同的实例具有相同的依赖属性值?;,c#,wpf,dependency-properties,C#,Wpf,Dependency Properties,当我试图在WPF/.NET3.5中开发自定义控件时,出现了一个严重的问题 细节 1.当我在同一窗口中添加两个实例时,第二个实例似乎总是具有第一个实例的依赖属性值,即使test2只是在不应用模板的情况下构建的(例如:tes2.TopItems/CenterItems/BottomItems与tes1相同,包括计数、项…) 2.我移除其中任何一个都可以 3.TopItems/CenterItems/BottomItems在OnApplyTemplate()中初始化 4.更改属性“Calendar”和

当我试图在WPF/.NET3.5中开发自定义控件时,出现了一个严重的问题

细节 1.当我在同一窗口中添加两个实例时,第二个实例似乎总是具有第一个实例的依赖属性值,即使test2只是在不应用模板的情况下构建的(例如:tes2.TopItems/CenterItems/BottomItems与tes1相同,包括计数、项…)

2.我移除其中任何一个都可以

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

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

5.“TopItems”、“CenterItems”、“BttomItems”用于控件模板中的{TemplateBinding}

6.我尝试使用“普通属性”{Binding RelativeSource=TemplatedParent}而不是“Depdency属性”和{TemplateBinding},效果很好真奇怪

拜托,有人能帮忙吗?谢谢! 类窗口 OnApplyTemplate() 人口项目()
private void PopulateItems()
{
开关(日历视图类型)
{
案例日历ViewType.Week\u Day:
{
foreach(Calendar.Items中的变量项)
{
如果(item.Date.DayOfWeek==DayOfWeek.Sunday)
(顶部项目为IList)。添加(项目);
}
CenterItems=BottomItems=Calendar.Items;
打破
}
案例日历ViewType.Month\u Week:
{
foreach(Calendar.Items中的变量项)
{
如果(item.Date.DayOfYear==1)
(顶部项目为IList)。添加(项目);
如果(item.Date.Day==1)
(中心项目作为IList)。添加(项目);
如果(item.Date.DayOfWeek==DayOfWeek.Monday)
(底部项目为IList)。添加(项目);
}
打破
}
案例日历ViewType.Year\u Month:
{
foreach(Calendar.Items中的变量项)
{
如果(item.Date.DayOfYear==1)
(顶部项目为IList)。添加(项目);
如果(item.Date.Day==1)
(中心项目作为IList)。添加(项目);
如果(item.Date.DayOfWeek==DayOfWeek.Monday)
(底部项目为IList)。添加(项目);
}
打破
}
}
}

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


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

您的哪个依赖项属性在窗口的两个实例上似乎具有相同的值?
OnApplyTemplate()
不会操作任何依赖项属性。
PopulateItems()
的内容是什么?实际输出与您期望的有多大差异?谢谢!但情况可能不同。e、 g:加载test1时,test1.Top/Center/Bottom有1/8/37项。然后test2开始构建,我发现test2.Top/Center/Bottom也有1/8/37项!有什么建议吗?谢谢!谢谢你的好伙伴!你完全正确!!!!!!!你救了我的命和午餐!!!再次感谢!!纯洁的小宝石!
<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>
    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); }
    }
 static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var calendarTile = (CalendarTitle)d;
        calendarTile.isLoaded = false;
        calendarTile.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;
                }
        }

    }