好吧,我不明白,我知道,這個問題已被問及至少回答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'
的最後顯示的數值...
任何想法這個問題?
謝謝。這可能是另一個問題,但代碼甚至沒有被調用,如果我使用我的代碼 – thardes2
按照慣例,靜態DependencyProperty字段也必須命名爲'XValueProperty'。 – Clemens