如何绑定一个控件的属性到另一个? [英] How to bind property of one control to another?

查看:169
本文介绍了如何绑定一个控件的属性到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF控件(ControlA),它引用另一个控制(ControlB)像这样:

 <电网>
    <控制:ControlB X:名称=ControlB/>
    <我:数据网格
        X:名称=dataGridBackup
        的ItemsSource ={结合}
        的AutoGenerateColumns =FALSE>
        <我:DataGrid.Columns>
            <我:DataGridCheckBoxColumn
                标题=连接的
                绑定={结合可连接}/>
        < /我:DataGrid.Columns>
    < /我:的DataGrid>
< /网格>
 

现在在ControlB我有一个按钮,我想IsEnabled属性绑定到我的网格上controlA的连接的列。

我可以得到它的工作时,我都在同一页上的控制,但不是在aboce场景。我试着用

 <按钮
    的IsEnabled ={结合的ElementName = dataGridBackup,路径= SelectedItem.Connectable}>
< /按钮>
 

解决方案

按钮在 ControlB 有没有正常的办法知道什么是无法控制的 ControlB 。一个可能的解决方案是布尔依赖属性添加到 ControlB IsConnectable 。在控制 ControlA 的XAML,绑定属性 dataGridBackup 与路径,像你一样。在控制 ControlB ,绑定的XAML按钮的的IsEnabled 属性 IsConnectable ControlB的

I have a WPF control (ControlA) which references another control (ControlB) like so:

<Grid>
    <controls:ControlB x:Name="ControlB"  />         
    <my:DataGrid 
        x:Name="dataGridBackup" 
        ItemsSource="{Binding}" 
        AutoGenerateColumns="False" >
        <my:DataGrid.Columns>
            <my:DataGridCheckBoxColumn 
                Header="Connectable" 
                Binding="{Binding Connectable}"  />
        </my:DataGrid.Columns>
    </my:DataGrid>
</Grid>

Now in ControlB I have a button and i want to bind the IsEnabled property to the Connectable column of my Grid on controlA.

I can get it working when i have the controls on the same page but not in the aboce scenario. I tried with

<Button 
    IsEnabled="{Binding ElementName=dataGridBackup, Path=SelectedItem.Connectable}">
</Button>

解决方案

Button inside ControlB has no normal way to know what's outside the control ControlB. One possible solution is to add boolean dependency property to ControlB like IsConnectable. In XAML of control ControlA, bind the property to dataGridBackup with path, like you did. In XAML of control ControlB, bind the button's IsEnabled property to IsConnectable of ControlB.

这篇关于如何绑定一个控件的属性到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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