数据绑定在WPF的父对象 [英] DataBinding in WPF for a parent object

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

问题描述

我有一个TreeView我填充并添加文本菜单到每个项目。现在的问题是在我的ViewModel TreeView中的ItemSource势必在视图模型本身的属性。当我试图引用的视图模型某些属性再次我似乎无法得到它的工作。

I have a TreeView I'm populating and adding a ContextMenu to each item. The problem is in my ViewModel the TreeView ItemSource is bound to a property on the ViewModel itself. When I attempt to reference some property on the ViewModel again I can't seem to get it to work.

<TreeView Grid.ColumnSpan="1" Grid.Row="1" HorizontalAlignment="Stretch" ItemsSource="{Binding ModelItems}" SelectedTreeItem="{Binding SelectedItem, Mode=TwoWay}" VerticalAlignment="Stretch" Grid.RowSpan="3" Margin="5">
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Models}">
        <TextBlock Text="{Binding Header, Mode=TwoWay}"  ToolTip="{Binding Tooltip, Mode=TwoWay}">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Server" Visibility="{Binding Path=IsServerVisible}">
                        <MenuItem Header="Add" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Windows:MainWindow}}, Path=ViewModel:ViewModel.AddServerCommand}"/>
                        <MenuItem Header="Edit" />
                        <MenuItem Header="Delete" />
                    </MenuItem>
                    <MenuItem Header="Config" Visibility="{Binding Path=IsConfigVisible}">
                        <MenuItem Header="Fetch" />
                        <MenuItem Header="Edit" />
                        <MenuItem Header="Save" />
                    </MenuItem>     
                </ContextMenu>
            </TextBlock.ContextMenu>
        </TextBlock>
    </HierarchicalDataTemplate>                
</TreeView.ItemTemplate>

在计算器上一个previous后指着我使用的RelativeSource正确绑定到我的主窗口视图模型的方向。然而,当我运行命令不使用的应用程序和输出窗口不产生任何约束力或XAML的错误,我可以看到。

A previous post on StackOverflow pointed me in the direction of using the RelativeSource to bind correctly to my ViewModel on the MainWindow. However when I run the application the command is not working and the Output window is not generating any binding or xaml errors that I can see.

基本上能见度绑定工作,因为这些属性在型号项存在。不过,我想要的一切,没有需要特别移动到视图模型的命令。

Basically the Visibility bindings work because those properties exist on the "Models" items. However I want everything to be moved to the ViewModel especially the Command.

任何人能发现我做了什么错误吗?

Can anyone spot what I've done incorrectly here?

推荐答案

这里要记住的关键是的上下文菜单是不可视树的一部分

The key thing to remember here is context menus are not part of the visual tree.

因此​​,他们不继承相同的源,因为它们属于对绑定的控件。处理这个问题的方法是绑定到文本菜单本身的目标位置。但既然你要绑定它司令部ViewModel类,请将的DataContext在标签您TextBlock的,并在命令中使用绑定这样的 -

Therefore they don't inherit the same source as the control they belong to for binding. The way to deal with this is to bind to the placement target of the ContextMenu itself. But since you want to bind it for Command in ViewModel class, place the DataContext in Tag of your TextBlock and use in your command Binding like this -

<HierarchicalDataTemplate ItemsSource="{Binding Models}">
    <TextBlock Text="{Binding Header}"
               Tag="{Binding DataContext, RelativeSource=
               {RelativeSource Mode=FindAncestor, AncestorType=Window}}">
         <TextBlock.ContextMenu>
              <ContextMenu>
                  <MenuItem Header="Server" Command="{Binding 
                            PlacementTarget.Tag.AddServerCommand,
                            RelativeSource={RelativeSource Mode=FindAncestor, 
                            AncestorType=ContextMenu}}"/>
               </ContextMenu>
          </TextBlock.ContextMenu>
      </TextBlock>
</HierarchicalDataTemplate>

使用其他类似的绑定像上面,它会工作,你想怎样。

Use for other bindings similarly like above and it will work how you want.

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

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