【发布时间】:2016-03-08 10:55:32
【问题描述】:
好的,我不明白,我知道,这个问题已经被问和回答了至少 10000 次......但也许我这里有某种特殊情况,或者我就是不明白。
我有一个名为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>
....
如您所见,我还在 TrackerControl.Content 标记中添加了一个 TextBlock。不幸的是,TextBlock 显示了正确的值,但我的用户控件中没有收到绑定。
我得到这个输出错误:
BindingExpression 路径错误:在“对象”“StatisticspopupViewModel”(HashCode=3740464)上找不到“DataPoint”属性。
BindingExpression:Path=DataPoint.X; DataItem='StatisticspopupViewModel' (HashCode=3740464);目标元素是'Statisticspopup'(名称='');目标属性是“XValue”(类型“Double”)
如果我看一下文本框,一切正常。
我认为它与Binding.Path 属性有某种关系,因为它试图访问StatisticspopupViewModel,这绝对是错误的。文本框的输出:
- 在级别 0 - 为 TrackerHitResult.DataPoint 找到访问器 ReflectPropertyDescriptor(DataPoint)
- 使用访问器将级别 0 的项目替换为 TrackerHitResult ReflectPropertyDescriptor(DataPoint)
- 使用 TrackerHitResult 获取级别 0 的值 ReflectPropertyDescriptor(DataPoint): 数据点
- 在级别 1 - 为 DataPoint.X 找到访问器 ReflectPropertyDescriptor(X)
- 使用访问器将级别 1 的项目替换为 DataPoint ReflectPropertyDescriptor(X)
- 从 DataPoint 获取级别 1 的值,使用 ReflectPropertyDescriptor(X): '9' TransferValue - 得到原始值 '9'
- TransferValue - 隐式转换器生成“9”
- TransferValue - 使用最终值“9”
最后显示值...
对这个问题有什么想法吗?
【问题讨论】:
标签: c# wpf xaml data-binding oxyplot