WPF数据绑定使用的RelativeSource和AncestorType [英] WPF Databinding with RelativeSource and AncestorType

查看:5675
本文介绍了WPF数据绑定使用的RelativeSource和AncestorType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一些结合code工作。 Bascially我要在网格的上下文菜单的元素的IsEnabled属性绑定在网格中的所选行的一个值。

我有这个工作:

 <我:DataGrid.ContextMenu>
                <文本菜单的DataContext ={结合PlacementTarget.SelectedItem,的RelativeSource = {的RelativeSource自}}>
                    <菜单项标题=授权访问的IsEnabled ={结合可连接}/>
                < /文本菜单>
            < /我:DataGrid.ContextMenu>
 

不过,我想这样做,这样一来,它不工作。它没有错误,但只是没有禁用菜单项。知道为什么吗?

 <我:DataGrid.ContextMenu>
                <文本菜单>
                    <菜单项标题=授权访问的IsEnabled ={绑定路径= SelectedItem.Connectable,的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType = {X:键入我:DataGrid中}}}/>
                < /文本菜单>
            < /我:DataGrid.ContextMenu>
 

解决方案

中尝试使用的ElementName约束力,而不是祖先结合文本菜单是网格的可视树一部分。

- 编辑 -

嗯,我错了。的ElementName约束力的(例如下面给出),也不能与文本菜单工作。这不是DataGrid的可视树的一部分。这就是为什么它不能看到DataGrid中,因此不能引用它。你将不得不使用第一方法来做到这一点。

任何原因,你不想这样做的?

 < D​​ataGrid.ContextMenu>
                <文本菜单的DataContext ={结合的SelectedItem,的ElementName = DataGrid1中}>
                    <菜单项标题=授权访问
                              的IsEnabled ={结合可连接}/>
                < /文本菜单>
            < /DataGrid.ContextMenu>
 

I am trying to get some binding code working. Bascially I want to bind the IsEnabled property of an element of my grid's context menu with a value of the selected row in the grid.

I have it working with this:

            <my:DataGrid.ContextMenu>
                <ContextMenu DataContext="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource Self}}"> 
                    <MenuItem Header="Grant Access" IsEnabled="{Binding Connectable}"/>
                </ContextMenu>
            </my:DataGrid.ContextMenu>

But I want to do it this way and it's not working. It doesn't error but just doesn't disable the menu item. Any idea why?

            <my:DataGrid.ContextMenu>
                <ContextMenu> 
                    <MenuItem Header="Grant Access" IsEnabled="{Binding Path=SelectedItem.Connectable, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type my:DataGrid}}}"/>
                </ContextMenu>
            </my:DataGrid.ContextMenu>

解决方案

Try using ElementName binding instead of Ancestor binding. ContextMenu is not part of Grid's visual tree.

--edit--

Ah, I was wrong. ElementName binding (example given below) would also not work with ContextMenu. It is not part of DataGrid's visual tree. That is why it cannot see that DataGrid and thus cannot reference it. You will have to do it using the first method.

Any reason why you don't want to do it that way?

            <DataGrid.ContextMenu>
                <ContextMenu DataContext="{Binding SelectedItem, ElementName=DataGrid1}">
                    <MenuItem Header="Grant Access"
                              IsEnabled="{Binding Connectable}" />
                </ContextMenu>
            </DataGrid.ContextMenu>

这篇关于WPF数据绑定使用的RelativeSource和AncestorType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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