Binding-Target 永远不会在 *.resources 中声明的自定义对象中设置;绑定评估失败

发布于 2024-08-16 00:35:01 字数 3692 浏览 9 评论 0原文

我有一个像这样的 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 控件(画布、标签...)的控件。

类Parameters、ParameterDefinition 和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 下的元素到元素绑定也工作得很好。 它编译了,看起来资源字典中 elementname 的范围没有问题。 我错过了什么?

编辑1: 我忘了提及 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 ==>同样的问题。

我也尝试过这样绑定:

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

但没有区别。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2024-08-23 00:35:01

我认为您的类应该从 Freezable 派生,否则就没有继承上下文:DataContext 不会传播并且绑定不起作用。请参阅这篇文章 了解更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文