将ItemsMenu绑定在ItemsControl内的Button内

时间:2014-12-16 17:16:10

标签: c# wpf

我在UserControl中有一个ItemsControl,而我的ItemsControl ItemTemplate包含一个按钮。我可以让按钮的绑定命令工作,但无法使按钮内的ContextMenu工作。有人能告诉我我的Binding应该如何看待我的按钮的ContextMenu吗?

<ItemsControl ItemsSource="{Binding People}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Margin="0" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Command="{Binding DataContext.ShowPeopleDetailsAction, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
                    CommandParameter="{Binding CommandParameterIndex}" Background="Black" Cursor="Hand" Margin="1,0,14,10">
                <Button.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Clear" Command="{Binding DataContext.ClearPeopleDetailsAction, RelativeSource={RelativeSource AncestorType=ItemsControl, Mode=FindAncestor}}" 
                                  CommandParameter="{Binding CommandParameterIndex}" />
                    </ContextMenu>
                </Button.ContextMenu>
                <Button.Template>
                    <ControlTemplate>
                        <Grid>
                            <Image Source="/Resources;component/Images/people.png" Width="112"/>
                            <Label Content="{Binding PowerSupplyName, FallbackValue=default}" 
                                   Visibility="{Binding IsConfigured, Converter={StaticResource booleanToStringConverter}, FallbackValue=Visible}" 
                                   Foreground="White" FontWeight="Bold" Margin="0,15,0,0" Padding="0" HorizontalAlignment="Center" MaxWidth="110" />
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

0 个答案:

没有答案