[英]Binding a context menu header to the selected item of a ListView
I am trying to bind the header of the context menu to a property of the selected item of the respective ListView
.我正在尝试将上下文菜单的 header 绑定到相应ListView
的选定项的属性。 The objects of the ItemsSource
have an IsDuplicate
property. ItemsSource
的对象具有IsDuplicate
属性。 Any idea what is wrong?知道有什么问题吗?
<ListView x:Name="AthletesListView" ItemsSource="{Binding FoundAthletes}">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Name="AddorEditAthleteMenuItem"
Header="{Binding SelectedItem.IsDuplicate,
ElementName=AthletesListView,
Converter={StaticResource FoundAthletesAddEditMenuItemConverter}}"
Click="AddAthleteMenuItem_Click"/>
</ContextMenu>
</ListView.ContextMenu>
Below the error message:错误信息下方:
System.Windows.Data Error: 4: Cannot find source for binding with reference 'ElementName=AthletesListView'. System.Windows.Data 错误:4:找不到与引用“ElementName = AthletesListView”绑定的源。 BindingExpression:Path=SelectedItem;绑定表达式:路径=选定项; DataItem=null;数据项=空; target element is 'MenuItem' (Name='AddorEditAthleteMenuItem');目标元素是'MenuItem'(名称='AddorEditAthleteMenuItem'); target property is 'Header' (type 'Object')目标属性是“标题”(类型“对象”)
The ContextMenu
is not part of the same visual tree as the associated ListView
, because it is displayed in a different window. ContextMenu
与关联的ListView
不是同一可视化树的一部分,因为它显示在不同的 window 中。 Consequently, relative source and element name bindings don't work.因此,相对源和元素名称绑定不起作用。
Instead, you can use the PlacementTarget
of the ContextMenu
, which is the ListView
.相反,您可以使用ContextMenu
的PlacementTarget
,即ListView
。
<MenuItem Name="AddorEditAthleteMenuItem"
Header="{Binding PlacementTarget.SelectedItem.IsDuplicate, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Converter={StaticResource FoundAthletesAddEditMenuItemConverter}}"
Click="AddAthleteMenuItem_Click"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.