【发布时间】:2020-03-10 11:48:32
【问题描述】:
我对使用 relativeSource 和祖先级别完全感到困惑。 相对源用于从其他元素获取源。但是要成功地做到这一点,您必须计算该元素的级别。 (如何调试?)这是 WPF 中最令人困惑的部分。
在我的示例中,我有上下文菜单,我想绑定数据源然后命令。绑定必须如何才能在我的虚拟机中获取命令?谢谢
<Page.DataContext>
<PDB:UsersViewModel x:Name="vm"/>
</Page.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--Page Header info content-->
<Grid Grid.Row="0">
<TextBlock Text="{Binding SelectedUser.Name}"/>
<TextBlock Text="{Binding ElementName=myGrd, Path=CurrentColumn.DisplayIndex}"/>
</Grid>
<!--Datagrid content-->
<DataGrid x:Name="myGrd"
SelectionMode="Single"
SelectionUnit="Cell"
CurrentItem="{Binding SelectedUser, Mode=TwoWay}"
CurrentColumn="{Binding CurrentColumn, Mode=TwoWay}"
IsReadOnly="True"
Grid.Row="1"
ItemsSource="{Binding FilteredUserList}"
AutoGenerateColumns="True"
CanUserAddRows="False"
>
<DataGrid.Resources>
<ContextMenu x:Key="ContextMenu">
<ContextMenu.Items>
<MenuItem Header="{Binding
RelativeSource={RelativeSource
FindAncestor,
AncestorType={x:Type Page},
AncestorLevel=4}, Path=vm}" />
</ContextMenu.Items>
</ContextMenu>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
</Style>
</DataGrid.CellStyle>
</DataGrid>
</Grid>
【问题讨论】:
-
是的,我确实读过这篇文章。但在我看来,这个解决方案是错误的。他们在说什么: 1. 在 A 类中,您引用了另一个要访问的 B 类。 2.在A类你有财产。然后将 B 类属性设置为 A 类属性,然后从 A 类属性中尝试访问 B 类属性...
-
如何在评论中写新行?
-
您可以在 meta.stackoverflow.com 上找到此类问题的答案:Is there a way to break line in comment?
-
关键词是“ContextMenu不在可视化树中”和“PlacementTarget”。
标签: wpf mvvm binding contextmenu