【发布时间】:2012-10-18 11:15:23
【问题描述】:
我有自己的课程:
public class PeriodContainerPanel:StackPanel
{
public PeriodContainerPanel()
: base()
{
addCollectionsToStackPanel();
}
private void addCollectionsToStackPanel()
{
this.Children.Clear();
if (PeriodsList!=null)
{
double minutes = PeriodsList.Count * (Properties.Settings.Default.EndTimeSpan - Properties.Settings.Default.StartTimeSpan).TotalMinutes;
foreach (ObservableCollection<PeriodBase> lst in PeriodsList)
{
this.Children.Add(new ChartUserControl(lst) { Minutes = minutes });
}
}
}
public List<ObservableCollection<PeriodBase>> PeriodsList
{
get { return (List<ObservableCollection<PeriodBase>>)GetValue(PeriodsListProperty); } //do NOT modify anything in here
set { SetValue(PeriodsListProperty, value); addCollectionsToStackPanel(); } //...or here
}
public static readonly DependencyProperty PeriodsListProperty =
DependencyProperty.Register(
"PeriodsList", //Must be the same name as the property created above
typeof(List<ObservableCollection<PeriodBase>>), //Must be the same type as the property created above
typeof(PeriodContainerPanel), //Must be the same as the owner class
new UIPropertyMetadata(
null //default value, must be of the same type as the property
));
}
我在UserControl 中使用这个DependencyProperty PeriodList 就像这样:
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<UI:PeriodContainerPanel PeriodsList="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=DataContext}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
我检查Convertor 是否有任何获取过程(如果有值)是的有值并且它是正确的,但它没有设置为PeriodsList 属性。什么是问题? P.S 如果代码有什么问题,请告诉我,我可以补充
【问题讨论】:
-
是忘记了 PeriodList 属性的定义还是没有在这里显示?
-
哦,对不起。我认为问题在于您在绑定完成之前在构造函数中调用 addCollectionsToStackPanel() 。这就是 PeriodList 为空的原因。尝试注册 Loaded 并在那里调用它。
标签: wpf binding dependency-properties stackpanel gridviewcolumn