【发布时间】: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