DependencyPropertyChanged回调事件未被触发

时间:2015-05-18 08:37:31

标签: c# wpf

我有一个名为UserControl的{​​{1}},它有一个名为MultiChartControl的依赖项属性。

MultiChartInputDetails

但是以下回调方法甚至不会被触发一次:

 public ChartsData MultiChartInputDetails
    {
        get { return (ChartsData)GetValue(MultiChartInputDetailsProperty); }
        set { SetValue(MultiChartInputDetailsProperty, value); }
    }

    public static readonly DependencyProperty MultiChartInputDetailsProperty =
        DependencyProperty.Register("MultiChartInputDetails", typeof(ChartsData), typeof(MultiChartControl), new UIPropertyMetadata(new PropertyChangedCallback(MultiChartInputDetailsChanged)));

主要主控:

private static void MultiChartInputDetailsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
    {
        MultiChartControl chart = d as MultiChartControl;
        if (chart != null)
        {
            if (chart.ChartGrid.Children != null)
                chart.ChartGrid.Children.Clear();                
            chart.InitilizeData();
        }

        MessageBox.Show("MultiChartInputDetailsChanged fired");
    }

1 个答案:

答案 0 :(得分:1)

这是因为DependencyProperty未设置为双向绑定。这样做如下:

DependencyProperty.Register("MultiChartInputDetails",
    typeof(ChartsData),
    typeof(MultiChartControl),
    new FrameworkPropertyMetadat(default(ChartsData), 
        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
        MultiChartInputDetailsChanged)

进一步检查是否存在绑定错误。如果您不想提供一个依赖属性,默认情况下执行双向绑定,那么您可以按如下方式编写bindinga:

<multicharting:MultiChartControl x:Uid="multicharting:MultiChartControl_1" 
    MultiChartInputDetails="{Binding Path=MultiChartsInputDetails, 
                                     Mode=TwoWay,
                                     ElementName=Chart,
                                     Converter={StaticResource DebugConverter}}"/>