none
WPF中TabItem的ContextMenu命令Binding无效的问题 RRS feed

  • 问题


  • <ControlTemplate x:Key="TabControlContentTemplate" TargetType="{x:Type TabControl}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ScrollViewer Grid.Row="0" x:Name="TabControlScroller" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" Style="{StaticResource TabHeaderScrollViewer}"> <TabPanel IsItemsHost="True" SnapsToDevicePixels="True" /> </ScrollViewer> <Border Grid.Row="1" BorderBrush="{StaticResource DefaultBrush}" BorderThickness="1"> <ContentPresenter ContentSource="SelectedContent" /> </Border> </Grid> </ControlTemplate> <Style TargetType="TabItem" x:Key="TabItemStyle"> <Setter Property="Margin" Value="0,3,0,0" /> <Setter Property="Height" Value="Auto" /> <Setter Property="MaxWidth" Value="300" /> <Setter Property="Padding" Value="5" /> <Setter Property="HorizontalAlignment" Value="Stretch" /> <Setter Property="VerticalAlignment" Value="Stretch" /> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Stretch" /> <Setter Property="SnapsToDevicePixels" Value="True" /> <Setter Property="HeaderTemplate"> <Setter.Value> <DataTemplate> <Grid Focusable="False" VerticalAlignment="Center" > <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Image Grid.Column="0" Focusable="False" Width="16" Height="16" Margin="3,0,3,0" Source="{Binding TabImageSource}" /> <ContentPresenter Focusable="False" Grid.Column="1" x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="0,3,0,3" Content="{Binding TabName}" /> <Button x:Name="CloseButton" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5,0,5,0" Style="{StaticResource CloseButtonStyle}" Visibility="Visible" Command="{Binding DataContext.TabItemViewModel.TabItemCloseCmd,RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" CommandParameter="{Binding}" /> </Grid> </DataTemplate> </Setter.Value> </Setter> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu Margin="0,0,0.4,0"> <MenuItem Header="关闭" Command="{Binding DataContext.TabItemViewModel.CloseTabCmd,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}}" /> <MenuItem Header="关闭所有页面" Command="{Binding DataContext.TabItemViewModel.CloseAllTabCmd,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}}" CommandParameter="{Binding Path=PlacementTarget.Parent, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}}" /> <MenuItem Header="除此之外全部关闭" Command="{Binding DataContext.TabItemViewModel.CloseOtherTabCmd,RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" CommandParameter="{Binding Path=PlacementTarget.Parent, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}}" /> </ContextMenu> </Setter.Value> </Setter> <Setter Property="Content" Value="{Binding UserControl}" /> </Style>


    <TabControl 
    	Name="tabControl"
    	ItemsSource="{Binding TabItemViewModel.TabItems}"
    	SelectedItem="{Binding TabItemViewModel.SelectedModel,Mode=TwoWay}"
    	ItemContainerStyle="{StaticResource TabItemStyle}"
    	Template="{StaticResource TabControlContentTemplate}">        
    </TabControl>

    在TabItem上添加右键菜单,右键菜单的绑定命令项选中无效,估计是Binding中
    DataContext.TabItemViewModel.CloseTabCmd,
    RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ContextMenu}}

    的寻址方式有问题,请问该怎么写才有效?

    • 已编辑 shoucang 2018年4月10日 7:09
    2018年4月10日 6:50

全部回复

  • 你好,

    我尝试用你的代码去建一个项目测试,但是报了很多错误,没法成功运行起来,我不知道你的viewmodel是怎样的,CommandParameter 绑定也有问题,请上传一个能完整重现你问题的代码案例到OneDrive上,这样我能快速帮你定位问题。

    谢谢


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年4月11日 7:30
    版主