绑定不工作WPF祖先 [英] Binding an Ancestor not working WPF

查看:205
本文介绍了绑定不工作WPF祖先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TreeView,被修改的文字面前展示形象了。所以,我的修改树型视图被称为ImagedTreeViewItem。
这ImagedTreeViewItem有一个属性,包含图像的图像控制显示。
该ImagedTreeViewItem也有一个特性,即检查,如果ImagedTreeViewItem图标是文件夹图标。此属性的名称为IsFolder。

我的问题是:
我绑定的祖先 - 属性(此处的ImagedTreeViewItem)来得到我需要的数据。对于我的图像,控制它的作品完美,因为我后来添加的不是一个上下文菜单。
我不明白为什么,因为基本上是相同的命令。

下面是我的XAMLImagedTreeView的code:

 < TreeView.Resources>
                <风格的TargetType ={X:类型myClasses:ImagedTreeViewItem}>
                    < setter属性=HeaderedItemsControl.HeaderTemplate>
                        < Setter.Value>
                            <&DataTemplate的GT;
                                < StackPanel的方向=横向>
<! - 在这里,它的工作原理! - >
                                    <图像高度=16来源={绑定路径=图标,的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType = {X:类型myClasses:ImagedTreeViewItem}}}拉伸=填充WIDTH =16/&GT ;
                                    < TextBlock的保证金=5,0文本={结合}/>
                                    < StackPanel.ContextMenu>
                                        <&文本菜单GT;
<! - 这里不是:( - >
                                            <菜单项命令=我:ImagedTreeView.AddFolder标题=添加文件夹
                                                              IsEnabled ={绑定路径= IsFolder,
                                                              的RelativeSource = {的RelativeSource模式= FindAncestor,AncestorType = {X:类型myClasses:ImagedTreeViewItem}}}>
                                                < MenuItem.Icon>
                                                    <图像源=folderadd16.png/>
                                                < /MenuItem.Icon>
                                            < /菜单项>
                                            <! - ... - >
                                        < /文本菜单>
                                    < /StackPanel.ContextMenu>
                                < / StackPanel的>
                            < / DataTemplate中>
                        < /Setter.Value>
                    < /二传手>
                < /样式和GT;
            < /TreeView.Resources>

我想在这个code找不到祖先的第二约束力。 Visual Studio中的输出窗口告诉我是一样的:

  System.Windows.Data错误:4:参照的RelativeSource FindAncestor,AncestorType ='绑定无法找到源.... ImagedTreeViewItem',AncestorLevel ='1'。 BindingEx pression:路径= IsFolder;的DataItem = NULL;目标元素是'菜单项(名称=''); target属性是IsEnabled(类型'布尔')


解决方案

文本菜单不是的VisualTree的一部分,这就是为什么绑定失败。你必须使用某种类型的继电器: ContextMenu.PlacementTarget 标签属性作为高速缓存的结合第二踪迹搜索。我认为这将工作:

 < StackPanel的方向=横向
            标签={绑定的RelativeSource = {的RelativeSource AncestorType = {X:类型myClasses:ImagedTreeViewItem}}}>
    < StackPanel.ContextMenu>
        <&文本菜单GT;
            <菜单项命令=我:ImagedTreeView.AddFolder标题=添加文件夹
                      IsEnabled ={绑定路径= PlacementTarget.Tag.IsFolder,的RelativeSource = {的RelativeSource AncestorType =文本菜单}}>
                < MenuItem.Icon>
                    <图像源=folderadd16.png/>
                < /MenuItem.Icon>
            < /菜单项>
            <! - ... - >
        < /文本菜单>
    < /StackPanel.ContextMenu>

I've got a TreeView, which is modified to show Images in front of the text too. So my modified TreeViewItem is called ImagedTreeViewItem. This ImagedTreeViewItem has a Property, that contains the Image for Image-Control to show. The ImagedTreeViewItem has also a property, that checks, if the ImagedTreeViewItem-Icon is a folder-Icon. This Property has the name "IsFolder".

My Problem is: I'm Binding the Ancestors-Property (here: The ImagedTreeViewItem) to get the data I need. For my Image-Control it works perfectly, for a Context Menu I added later not. I don't understand why, because basically it is the same command.

Here is the Code of my "ImagedTreeView" in XAML:

<TreeView.Resources>
                <Style TargetType="{x:Type myClasses:ImagedTreeViewItem}">
                    <Setter Property="HeaderedItemsControl.HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
<!-- Here it works!!! -->
                                    <Image Height="16" Source="{Binding Path=Icon, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type myClasses:ImagedTreeViewItem}}}" Stretch="Fill" Width="16" />
                                    <TextBlock Margin="5,0" Text="{Binding}" />
                                    <StackPanel.ContextMenu>
                                        <ContextMenu>
<!-- Here not :( -->
                                            <MenuItem Command="my:ImagedTreeView.AddFolder" Header="Add Folder"
                                                              IsEnabled="{Binding Path=IsFolder,
                                                              RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type myClasses:ImagedTreeViewItem}}}">
                                                <MenuItem.Icon>
                                                    <Image Source="folderadd16.png" />
                                                </MenuItem.Icon>
                                            </MenuItem>
                                            <!-- ... -->
                                        </ContextMenu>
                                    </StackPanel.ContextMenu>
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TreeView.Resources>

I think the second binding in this code cannot find the ancestor. The output-window of Visual Studio tells me the same:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='....ImagedTreeViewItem', AncestorLevel='1''. BindingExpression:Path=IsFolder; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'IsEnabled' (type 'Boolean')

解决方案

ContextMenu is not part of the VisualTree, that's why the binding fails. You have to use some kind of relay: ContextMenu.PlacementTarget and Tag property as a cache for the second trail of binding search. I think this will work:

<StackPanel Orientation="Horizontal"
            Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type myClasses:ImagedTreeViewItem}}}">
    <StackPanel.ContextMenu>
        <ContextMenu>
            <MenuItem Command="my:ImagedTreeView.AddFolder" Header="Add Folder"
                      IsEnabled="{Binding Path=PlacementTarget.Tag.IsFolder, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
                <MenuItem.Icon>
                    <Image Source="folderadd16.png" />
                </MenuItem.Icon>
            </MenuItem>
            <!-- ... -->
        </ContextMenu>
    </StackPanel.ContextMenu>

这篇关于绑定不工作WPF祖先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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