【问题标题】:Why this Binding doesn't work properly even it gets value from source?为什么这个 Binding 即使从源代码中获取价值也不能正常工作?
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 我知道 CallBack 方法,但它必须是静态的,并且会产生额外的代码,我必须将其他东西设置为静态才能使用我的方法。这是正确的解决方案吗?
  • 如果你想对被改变的依赖属性做出反应,你基本上需要使用回调,是的。
【解决方案2】:

正如@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 sn-p 不是资源中的控件模板,对吧?

【讨论】:

    猜你喜欢
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2014-01-06
    相关资源
    最近更新 更多