简体   繁体   English

使用RelativeSource和AncestorType进行WPF数据绑定

[英]WPF Databinding with RelativeSource and AncestorType

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. 基本上我想将网格上下文菜单元素的IsEnabled属性与网格中选定行的值绑定。

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 . 尝试使用ElementName binding而不是Ancestor binding ContextMenu is not part of Grid's visual tree. ContextMenu不是Grid的可视化树的一部分。

--edit-- - 编辑 -

Ah, I was wrong. 啊,我错了。 ElementName binding (example given below) would also not work with ContextMenu. ElementName绑定(下面给出的示例)也不适用于ContextMenu。 It is not part of DataGrid's visual tree. 它不是DataGrid可视化树的一部分。 That is why it cannot see that DataGrid and thus cannot reference it. 这就是为什么它无法看到DataGrid因此无法引用它。 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>

如果在调试模式下查看Visual Studio中的输出窗口,您将获得绑定错误的详细信息 - 这可能会对您的问题有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用RelativeSource和AncestorType进行WPF绑定 - WPF Binding with RelativeSource and AncestorType 是否可以使用AncestorType指向基类型的WPF RelativeSource绑定? - Is possible to make a WPF RelativeSource binding with AncestorType pointing to a base type? 使用MVVM的WPF:使用RelativeSource的DataBinding - WPF using MVVM: DataBinding with RelativeSource 具有相对源的数据绑定依赖属性 - Databinding-dependency property with Relativesource MVVM WPF与RelativeSource的绑定 - MVVM WPF bindings with RelativeSource WPF绑定相对源 - WPF Binding RelativeSource 找不到参考&#39;RelativeSource FindAncestor,AncestorType =&#39;System.Windows.Controls.UserControl&#39;,AncestorLevel =&#39;1&#39;&#39;的绑定源 - Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1'' 无法找到引用绑定的源'RelativeSource FindAncestor,AncestorType ='MahApps.Metro.Controls.Glow',AncestorLevel ='1'' - Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1'' 在 WPF 中使用 RelativeSource FindAncestor 时出错 - Error Using RelativeSource FindAncestor in WPF WPF XML 绑定到RelativeSource PreviousSibling? - WPF XML Bind to RelativeSource PreviousSibling?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM