根绑定属性在XAML子元素的值 [英] Bind property of root to value of child element in XAML

查看:182
本文介绍了根绑定属性在XAML子元素的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是太习惯WPF,所以这可能是一些容易,但我一直在用它挣扎了几个小时,似乎无法得到如何正确地做到这一点。

I'm not too used to WPF, so this is probably something easy, but I've been struggling with it for a couple hours and can't seem to get how to properly do it.

说我有一个 BaseUserControl 用户控件降了依赖属性文本

Say I have a BaseUserControl descending from UserControl with a dependency property Text.

然后在XAML我创建一个BaseUserControl后代。我想该财产文本绑定到在后代中定义的控件。你说:

Then in XAML I'm creating a BaseUserControl descendant. I want that property Text to be bound to a control defined in that descendant. Say:

<base:BaseUserControl
         ... all namespaces ...
         xmlns:base="clr-namespace:MyControlsBase"
         x:Class="Test.MyTestControl"
         Text="{Binding ElementName=MyTextBox, Path=Text}"
    <TextBox x:Name="MyTextBox" Text="MyText" />
</base:BaseUserControl>

由于某些原因,我不能让 MyTextBox 更新文本属性控件本身。

如果我添加一个:

<TextBlock Text="{Binding ElementName=MyTextBox, Path=Text}" />

任何地方控制里面,文本块显示正确的文本框的值,因此绑定定义似乎并不成为问题。

Anywhere inside the control, the textblock shows the correct TextBox value so the binding definition doesn't seem to be the problem.

我有别的东西这说明在控制文本的价值......这样说:

I have something else which shows the value of Text in that control... say something like:

<Window>
  <StackPanel>
    <test:MyTestControl x:Name="MyControl" />
    <TextBlock Text="{Binding ElementName=MyControl, Path=Text}" />
  </StackPanel>
</Window>

如果我更新从任何其他途径上MyControlBase Text属性(codebehind,或其他),它的工作原理,我看到改变的文本块文本...但它不工作时好像更新内部本身的文本框将被更新。

If I update the Text property on MyControlBase from any other means (codebehind, or whatever), it works, and I see the text changed on the textblock... but it doesn't work seem to update when the TextBox inside itself is updated.

是否有任何限制的,当你继承控件绑定到属性?

Are there any limitations on binding to properties when you are inheriting a control?

附:code显然是人为的,boilerplated这个问题

注意:显然有一些错误与该属性的结合,因为在跟踪窗口上,创建控件时,我得到一个:

Note: there is obviously something wrong with the binding on that property, since on the trace window, when creating the control, I get a:

System.Windows.Data错误:4:无法为参照的ElementName = MyTextBox'绑定找到源头。 BindingEx pression:路径=文本;的DataItem = NULL;目标元素是'MyTestControl'(名称='');目标属性是'文本'(类型'字符串')​​

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MyTextBox'. BindingExpression:Path=Text; DataItem=null; target element is 'MyTestControl' (Name=''); target property is 'Text' (type 'String')

但它只会发生在`MyTestControl的财产,而不是为了其他任何具有约束力的XAML里面相同的属性。

But it only happens for the `MyTestControl's property, and not for any other binding to the same property inside the XAML.

推荐答案

我相信问题是,当BaseUserControl初始化自己,并尝试与MyTextBox的Text属性绑定MyTextBox尚未初始化。在这个阶段,MyTextBox不存在,结果你得到的System.Windows.Data错误:4:找不到 作为参考结合。

I believe the problem is that the MyTextBox hasn't been initialized when the BaseUserControl initializes itself and tries to bind with the Text property of the MyTextBox. At this stage, the MyTextBox doesn't exist, as a result you get the 'System.Windows.Data Error: 4 : Cannot find SOURCE for binding with reference'.

您可以在InitializeComponent(后C-背后$ C $)在MyTestControl的CTOR绑定。

You can bind in code-behind after the InitializeComponent() in the CTOR of your MyTestControl.

public MyTestControl()
{
   InitializeComponent();
   Binding b = new Binding("Text");
   b.Source = MyTextBox;
   SetBinding(TextProperty, b);
}

这篇关于根绑定属性在XAML子元素的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆