揭露儿童控制依赖属性在WPF绑定 [英] Exposing Child Control Dependency Properties for Binding in WPF

查看:180
本文介绍了揭露儿童控制依赖属性在WPF绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的用户控件名为 CustomTextBox 此XAML:

I have a very simple UserControl called CustomTextBox with this XAML:

<UserControl x:Class="CustomTextBox" ... >
    <Grid>
        <TextBox x:Name="InnerTextBox"/>
    </Grid>
</UserControl>

现在,当我使用 CustomTextBox 和想做的事情结合 InnerTextBox.Text ,这是行不通的:

Now when I use CustomTextBox and want to do binding to InnerTextBox.Text, it does not work:

... {Binding ElementName=CustomTextBox, Path=InnerTextBox.Text}

我尝试了另一种方式,不能正常工作还有:

I tried it another way, does not work as well:

... {Binding ElementName=CustomTextBox.InnerTextBox, Path=Text}

我知道我可以定义的依赖所谓的财产 CustomTextBox.Text ,然后将其绑定到 InnerTextBox.Text ,但我计划有自定义控件具有许多特性,这是一个工作拷贝个个只是为了支持绑定的地狱。此外,复制/包裹性是指每个值存储两次。

I know I can define a new dependency property called CustomTextBox.Text and then bind it to InnerTextBox.Text but I am planning to have custom controls with many properties and it is hell of a work to copy all of them just to support binding. Furthermore, copying/wrapping properties means each value is stored twice.

在的WinForms,这是一个问题简单的继承和所有属性自动可用。在WPF,XAML控件的继承是不可能的,属性是不可访问。

In WinForms, this was a matter simple inheritance and all the properties were available automatically. In WPF, inheritance of XAML controls is not possible and the properties are inaccessible.

是否有关于如何设置一些控制结合用户控件的子元素属性没有简单的方法?

Is there any simple way on how to set up binding from some control to UserControl's child element property?

推荐答案

1.in你的控制背后的code创建一个新的DP,说我们把它从XAML文本
2.您code:

1.in the code behind of your control create a new DP ,say we call it Text
2. in the xaml from your code :

<TextBox x:Name="InnerTextBox" Text={Binding Text}/> 


3.确保该文本框的DataContext的是用户控件


3.make sure that the DataContext of the textBox is the UserControl

这篇关于揭露儿童控制依赖属性在WPF绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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