0
我想通過列表框的選定索引屬性作爲命令參數到上下文菜單項,我有命令綁定工作(感謝威爾@ElementName Binding from MenuItem in ContextMenu),但我'我的命令參數有問題。從上下文菜單項獲取父列表框選擇索引作爲CommandParamater項目
<UserControl>
<ListBox ItemsSource="{Binding myItems}">
<ListBox.Resources> <!-- The selected item is the item the mouse is over -->
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}"
Value="True">
<Setter Property="IsSelected" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="Edit" Grid.Column="4" Grid.Row="0" Tag="{Binding DataContext, ElementName=ProductBacklog}">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove"
Command="{Binding PlacementTarget.Tag.RemoveStoryClickCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
CommandParameter="{Binding <!--I NEED TO BIND TO THE LISTBOX-->, Path=SelectedIndex}"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl>
感謝您的幫助,我但在我原來的問題中出現錯誤,這是我需要的項目索引,而不是實際的項目。這可能嗎? –
@EamonnMcEvoy Hrrrm可能將您的Button的DataContext設置爲指向ListBox的SelectedIndex的RelativeSource綁定? '' – Rachel
它工作:)非常感謝! –