当我尝试在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>
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;
}
}
}
答案 0 :(得分:11)
指定依赖项属性的默认值时,将在所有实例之间共享此默认值。因此,如果它是一个可变的引用类型,就像一个集合,对该集合的任何更改都将反映在该类的所有实例中。
您应该将默认值保留为null
,并在类构造函数中初始化集合。这样,该类的所有实例都将拥有自己的集合。