DataBinding in WPF for a parent object

[亡魂溺海] 提交于 2020-01-14 04:58:25

问题


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>

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?


回答1:


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

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.




回答2:


Because you didn't post the full .xaml, lets use the datacontext of the treeview. Because The RelativeSource is giving us access to the TreeView, you need to use DataContext in the Binding Path since that is a reference to your ViewModel.

Command="{Binding Path="DataContext.AddServerCommand" RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}}"



回答3:


Instead of

<MenuItem Header="Add" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Windows:MainWindow}}, Path=ViewModel:ViewModel.AddServerCommand}"/>

Try

<MenuItem Header="Add" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Windows:MainWindow}}, Path=DataContext.AddServerCommand}"/>

Isn't your ViewModel your DataContext?



来源:https://stackoverflow.com/questions/13390671/databinding-in-wpf-for-a-parent-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!