2015-05-18 165 views
2

我有一個UserControl名爲MultiChartControl,它有一個名爲MultiChartInputDetails的依賴項屬性。DependencyPropertyChanged回調事件沒有被觸發

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

和主主控:

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

是否結合產生一個有效的價值? – Clemens

回答

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}}"/> 
+0

觸發依賴項屬性的PropertyChangedCallback不需要雙向綁定。 – Clemens

+0

@Clemens否,那不是,但是如果綁定變量已經改變並且沒有TwoWayBinding,則DP的值將不會被設置。 –

+0

這是不正確的。你不知道綁定的源屬性(不幸的是也被命名爲MultiChartsInputDetails),因爲OP沒有告訴我們。但是,如果它更改並引發更改通知,則目標MultiChartsInputDetails屬性當然會更改,即使是單向綁定。 – Clemens