WPF中Popup和ContextMenu类无法以常规方式绑定数据和命令,但只能通过参数PlacementTarget获得父控件
比如
<Window x:Name="window"> <ItemsControl Items="{Binding BookItems}"> <ItemsControl.ItemTemplate> <DataTemplate> <Button Style="{DynamicResource CleanButtonStyle}" Padding="5,0" Click="LoadFavorite" Content="{Binding Title}"> <Button.ContextMenu> <ContextMenu> <MenuItem Header="删除" Command={Binding DataContext.DeleteCommand,Element=window}" /> </ContextMenu> </Button.ContextMenu> </Button> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Window>
比如这里的Command就是无效的。