2016-03-08 167 views
1

好吧,我不明白,我知道,這個問題已被問及至少回答10000次....但也許我在這裏有某種特殊情況,或者我只是不知道沒辦法。WPF:用戶控件的依賴屬性

我有一個用戶控件是被稱爲Statisticspopup,它有一個DependencyProperty如下所示:

public static readonly DependencyProperty XValueProperty = DependencyProperty.Register(
    "XValue", typeof(double), typeof(Statisticspopup), 
    new FrameworkPropertyMetadata(XValueChanged)); 

public double XValue 
{ 
    get 
    { 
     var x = GetValue(XProperty); 
     return (double)x; 
    } 
    set 
    { 
     SetValue(XProperty, value); 
    } 
} 

private static void XValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    var control = (Statisticspopup)d; 
    control.XValue = double.Parse(e.NewValue.ToString()); 
    System.Diagnostics.Debug.WriteLine("XValueChanged"); 
} 

我用它在我的XAML代碼:

<controls:Statisticspopup XValue="42" /> 

這個工作和一切很好......現在我想使用綁定的屬性,如下所示:

<controls:Statisticspopup XValue="{Binding DataPoint.X,PresentationTraceSources.TraceLevel=High}" /> 

的DataPoint.X值從另一個控制(OxyPlot對象),所以整個代碼如下所示:

<oxy:Plot x:Name="PlotThing" Title="{Binding Title}" Style="{DynamicResource PlotStyle}" > 
    <oxy:Plot.TrackerDefinitions> 
     <oxy:TrackerDefinition TrackerKey="someKey" > 
      <oxy:TrackerDefinition.TrackerTemplate> 
       <ControlTemplate> 
        <oxy:TrackerControl Name="TrackerControl" DataContext="{Binding }" Position="{Binding Position}" LineExtents="{Binding PlotModel.PlotArea}"> 
         <oxy:TrackerControl.Content>  

<controls:Statisticspopup XValue="{Binding DataPoint.X,PresentationTraceSources.TraceLevel=High}" /> 
<TextBlock Foreground="Aquamarine" Text="{Binding DataPoint.X, PresentationTraceSources.TraceLevel=High}"></TextBlock> 
.... 

正如你所看到的,我還添加了一個TextBlock到TrackerControl.Content標籤。不幸的是,TextBlock顯示正確的值,但我沒有收到我的用戶控件中的綁定。

我得到這個輸出誤差:如果我看看到TextBox

BindingExpression path error: 'DataPoint' property not found on 'object' ''StatisticspopupViewModel' (HashCode=3740464)'.
BindingExpression:Path=DataPoint.X; DataItem='StatisticspopupViewModel' (HashCode=3740464); target element is 'Statisticspopup' (Name=''); target property is 'XValue' (type 'Double')

,一切正常。

我認爲它與Binding.Path屬性有某種關係,因爲它試圖訪問StatisticspopupViewModel,這肯定是錯誤的。從文本框的輸出:

  • 在級別0 - 用於TrackerHitResult.DataPoint發現存取 ReflectPropertyDescriptor(數據點)
  • 在與TrackerHitResult 0級替換項,利用存取器 ReflectPropertyDescriptor(數據點)
  • 的GetValue在從TrackerHitResult使用 ReflectPropertyDescriptor(數據點)0級:數據點
  • 在級別1 - 爲DataPoint.X發現存取 ReflectPropertyDescriptor(X)
  • 在與數據點1級替換項,使用 ReflectPropertyDescriptor(X),使用訪問 ReflectPropertyDescriptor(X)
  • 的GetValue處第1級從數據點: '9' TransferValue - 得到原始值 '9'
  • TransferValue - 隱式轉換器產生 '9'
  • TransferValue - 使用最終值 '9'

的最後顯示的數值...

任何想法這個問題?

回答

0

好的,我得到了它的工作。如果我刪除了ViewModel綁定,代碼就可以設置依賴屬性並以正確的方式使用它們。 This post here解釋如何做到這一點。

0

您的PropertyChangedCallback已損壞。它不能包含該行

control.XValue = double.Parse(e.NewValue.ToString()); 

哪btw。應該看起來像control.XValue = (double)e.NewValue;

該方法是XValue屬性的「更改」回調,因此在屬性值已更改時調用。它不應該(也不一定)再次設置該值,因爲這會有效地從屬性中刪除綁定。

private static void XValueChanged(
    DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    var control = (Statisticspopup)d; 

    // here control.XValue is already identical to (double)e.NewValue; 

    Debug.WriteLine("XValueChanged: {0}, {1}", e.NewValue, control.XValue); 
} 
+0

謝謝。這可能是另一個問題,但代碼甚至沒有被調用,如果我使用我的代碼 – thardes2

+1

按照慣例,靜態DependencyProperty字段也必須命名爲'XValueProperty'。 – Clemens