Binding-Target never gets set in custom objects declared in *.resources; binding evaluation fails

380 views Asked by At

I have a xaml view like this:

<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"/>

/*SOME MORE CODE */

all the controls extending some standard wpf controls (canvas, label, ...).

The classes Parameters, ParameterDefinition & ValueBinding look like this:

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

The problem is that the databinding

Value="{Binding ElementName=myTextBox, Path=Text}"

not works in ValueBinding (Value is never set, has always the default value (and actions are performed to change the value)). The StaticResource binding in the dataprovider works perfectly, and element-to-element binding under the UIElements also works perfectly. It compiles, and it seems there's no problem with the scope of elementname in the resourcedictionary. What did i miss?

Edit1: I forgot to mention the output of the 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')

In the TvPanel the DataContext is set to this.

I tried to derive from Freezable, instead of DependencyObject ==> same Problem.

I also tried to bind this way:

        <Tv:ValueBinding>
            <Tv:ValueBinding.Value>
                <Binding ElementName="SearchText" Path="Text"/>
            </Tv:ValueBinding.Value>
        </Tv:ValueBinding>

but no difference.

1

There are 1 answers

1
Julien Lebosquain On

I think that your class should derive from Freezable, otherwise there's no inheritance context: the DataContext is not propagated and the bindings don't work. See this article for more information.