gpt4 book ai didi

c# - Windows.Resource中通过Multibinding通过ContextMenu获取ListBox SelectedItem

转载 作者:行者123 更新时间:2023-11-30 22:55:17 24 4
gpt4 key购买 nike

我正在尝试对存储在 Window.Resources 中的上下文菜单使用多重绑定(bind),但我不知道如何获取我将菜单附加到的 ListBox 的 SelectedItem。

我想我需要使用 PlacementTarget,但无法正常工作。我尝试使用 FindAncestor,但没有用。我还尝试通过 ElementName 获取列表框,但无济于事。

<Window ...>
<Window.DataContext>
<local:IPViewModel/>
</Window.DataContext>
<Window.Resources>
<sys:Int32 x:Key="Number">0</sys:Int32>
<sys:Int32 x:Key="NumberLetter">1</sys:Int32>
<sys:Int32 x:Key="NumberLetterNumber">2</sys:Int32>

<local:CombineParams x:Key="CombineParams"/>

<ContextMenu x:Key="DetailMenu">
<MenuItem Header = "Number">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding SwitchLabelMode}">
<i:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource CombineParams}">
<!-- The next line is where I'm not sure what to write to get to the listbox -->
<Binding Source="{RelativeSource AncestorType={x:Type ListBox}}"/>
<Binding Source="{StaticResource Number}"/>
</MultiBinding>
</i:InvokeCommandAction.CommandParameter>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</MenuItem>
...
</Window.Resources>

...
<ListBox Name="IndexDetailsListBox"
...
ContextMenu="{StaticResource DetailMenu}"
...>

</ListBox>
...
</Window>

除了未设定的值(value),我无法让它回馈任何东西。我很感激任何帮助。我发现的其他问题都没有 Window.Resources 中的上下文菜单和 MultiBinding,这似乎是我的问题。

最佳答案

您的问题是,您将 RelativeSource- 对象分配给 Binding 对象的 Source 属性。 RelativeSource- 对象属于 RelativeSource-property。
因此,您将 List 获取到多值转换器:

<MultiBinding Converter="{StaticResource CombineParams}">
<Binding Path="PlacementTarget" RelativeSource="{RelativeSource AncestorType={x:Type ContextMenu}}"/>
<Binding Source="{StaticResource Number}"/>
</MultiBinding>

关于c# - Windows.Resource中通过Multibinding通过ContextMenu获取ListBox SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55379343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com