0

我有这个自己的课程:

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
            ));
}

我像这样使用DependencyProperty PeriodListUserControl

<GridViewColumn>
   <GridViewColumn.CellTemplate>
   <DataTemplate>
      <UI:PeriodContainerPanel PeriodsList="{Binding RelativeSource={RelativeSource  Mode=TemplatedParent}, Path=DataContext}" />
   </DataTemplate>
   </GridViewColumn.CellTemplate>
</GridViewColumn>

我检查Convertor是否有任何获取过程(如果有价值)是的有价值并且它是正确的,但它没有设置为PeriodsList属性。什么是问题?PS如果对代码有任何疑问,请告诉我,我可以添加

4

2 回答 2

0

正如@MrDosu 建议的那样,在 DependencyProperty 上使用 PropertyChangedCallback。Callback 是静态的问题并不是什么大问题,因为您将在调用中获得对 DependencyObject 的引用:

private static void ValueChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  ((MyCustomControl)d).RaiseMyValueChanged(); //Where RaiseMyValueChanged is a private non-static method.
}

此外,您对 TemplatedParent 和 Path=DataContext 的使用似乎很奇怪。路径应该是指一个属性。TemplatedParent 用于定义样式和/或控件模板时,您的 XAML 片段不是资源中的控件模板,对吗?

于 2012-10-18T12:23:36.750 回答
0

addCollectionsToStackPanel()绑定发生时不会被调用。绑定引擎SetValue直接使用,这就是为什么你不应该在属性中做这样的逻辑。(这就是为什么它在自动生成的评论中说“不要修改任何东西”)

PropertyChangedCallback在这种情况下使用 a :http: //msdn.microsoft.com/en-us/library/ms745795.aspx

于 2012-10-18T12:52:18.923 回答