我有这样的xaml视图:
<Tv:TvPanel
xmlns:Tv="...">
<Tv:TvPanel.Resources>
<Tv:Parameters x:Key="parameterList">
<Tv:ParameterDefinition ParameterName="MyParam" InitialValue="123abc">
<Tv:ValueBinding Value="{Binding ElementName=myTextBox, Path=Text}"/>
</Tv:ParameterDefinition>
</Tv:Parameters>
<Tv:TvXmlDataProvider x:Key="dataProvider"
Source="http://server/myXml.xml"
Parameters="{StaticResource parameterList}"/>
</Tv:TvPanel.Resources>
<Tv:TvPage DataContext="{StaticResource dataProvider}" >
<Tv:TvText x:Name="myTextBox"
Tv:TvPage.Left="360" Tv:TvPage.Top="10"
Height="30" Width="200"/>
<Tv:TvButton Tv:TvPage.Left="560" Tv:TvPage.Top="10"
Content="Search"/>
/ *更多代码* /
所有控件都扩展了一些标准的wpf控件(canvas,label,...)。
类Parameters,ParameterDefinition&amp; ValueBinding看起来像这样:
[ContentProperty("ParameterDefinitions")]
public class Parameters
{
private readonly List<ParameterDefinition> parameterDefinitions = new List<ParameterDefinition>();
public List<ParameterDefinition> ParameterDefinitions { get { return parameterDefinitions; } }
}
[ContentProperty("ValueBindings")]
public class ParameterDefinition : DependencyObject
{
public static DependencyProperty ParameterNameProperty = DependencyProperty.Register("ParameterName", typeof (string), typeof (ParameterDefinition), new PropertyMetadata(""));
public string ParameterName
{
get { return (string) this.GetValue(ParameterNameProperty); }
set{ this.SetValue(ParameterNameProperty, value);}
}
public static DependencyProperty InitialValueProperty = DependencyProperty.Register("InitialValue", typeof(object), typeof(ParameterDefinition), new PropertyMetadata(new object()));
public object InitialValue
{
get { return this.GetValue(InitialValueProperty); }
set { this.SetValue(InitialValueProperty, value); }
}
private readonly List<ValueBinding> valueBindings = new List<ValueBinding>();
public List<ValueBinding> ValueBindings { get { return this.valueBindings; } }
}
public class ValueBinding : DependencyObject
{
public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(ValueBinding), new PropertyMetadata(""));
public string Value
{
get { return (string)this.GetValue(ValueProperty); }
set { this.SetValue(ValueProperty, value); }
}
}
问题在于数据绑定
Value="{Binding ElementName=myTextBox, Path=Text}"
在ValueBinding中不起作用(从不设置值,始终使用默认值(并执行操作以更改值))。数据提供者中的StaticResource绑定工作得很好,UIElements下的元素到元素绑定也很有效。 它编译,似乎resourcedictionary中的elementname范围没有问题。 我错过了什么?
EDIT1: 我忘了提到VS-Console的输出:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Text; DataItem=null; target element is 'ValueBinding' (HashCode=14342221); target property is 'Value' (type 'String')
在TvPanel中,DataContext设置为此。
我试图从Freezable派生,而不是DependencyObject ==&gt;同样的问题。
我也尝试用这种方式绑定:
<Tv:ValueBinding>
<Tv:ValueBinding.Value>
<Binding ElementName="SearchText" Path="Text"/>
</Tv:ValueBinding.Value>
</Tv:ValueBinding>
但没有区别。