如何重写Xceed.Wpf.AvalonDock LayoutItem在Header右键菜单的关闭命令
定义资源:
<ResourceDictionary> <xcad:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" /> <ContextMenu x:Key="DocumentContextMenu"> <MenuItem Command="{Binding Path=Model.CloseCommand}" Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}"> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="关闭" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem>
....
完整代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<xcad:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" /> <ContextMenu x:Key="DocumentContextMenu"> <MenuItem Command="{Binding Path=CloseCommand}" Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}"> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="关闭" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem> <MenuItem Command="{Binding Path=CloseAllButThisCommand}"> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="除此之外全部关闭" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem> <MenuItem Command="{Binding Path=CloseAllCommand}"> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="全部关闭" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem> <MenuItem Command="{Binding Path=FloatCommand}"> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="漂浮" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem> <MenuItem Command="{Binding Path=DockAsDocumentCommand}"> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="停靠成标签式文档" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem> <MenuItem Command="{Binding Path=NewHorizontalTabGroupCommand}" Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}"> <MenuItem.Icon> <Image Source="Images/HTabGroup.png" Stretch="Uniform" Width="14" /> </MenuItem.Icon> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="新建水平标签分组" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem> <MenuItem Command="{Binding Path=NewVerticalTabGroupCommand}" Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}"> <MenuItem.Icon> <Image Source="Images/VTabGroup.png" Stretch="Uniform" Width="14" /> </MenuItem.Icon> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="新建垂直标签分组" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem> <MenuItem Command="{Binding Path=MoveToNextTabGroupCommand}" Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}"> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="移动到下一个标签组" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem> <MenuItem Command="{Binding Path=MoveToPreviousTabGroupCommand}" Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}"> <MenuItem.HeaderTemplate> <DataTemplate> <TextBlock Text="移动到上一个标签组" /> </DataTemplate> </MenuItem.HeaderTemplate> </MenuItem> </ContextMenu>
注意这里的Model.CloseCommand的Model其实是DockingManager的DocumentsSource集合中的一项,该项对应一个含有CloseCommand属性的类实例,这样写其实就相当于覆盖掉原有CloseCommand
<xcad:DockingManager x:Name="dockingManager" AllowMixedOrientation="True" DocumentContextMenu="{StaticResource DocumentContextMenu}" BorderBrush="Black" FontFamily="Microsoft YaHei" DocumentClosing="dockingManager_OnDocumentClosing" DocumentsSource="{Binding Path=PanelsModel.Panels}" BorderThickness="1">
如DocumentContextMenu="{StaticResource DocumentContextMenu}"所示,直接指定DocumentContextMenu就ok了。
写得有点乱,权当是个简记。