如何获取关闭按钮以始终显示

时间:2017-11-07 10:30:08

标签: c# .net wpf user-interface avalondock

我正在使用AvalonDock创建标签。当鼠标悬停在选项卡上时,显示相应选项卡的关闭按钮,但是如果移除鼠标,关闭按钮将消失(并且颜色会更改)。有没有办法让关闭按钮一直显示?

<xcad:DockingManager.DocumentHeaderTemplate>
    <DataTemplate>
        <StackPanel Height="20" Orientation="Horizontal" VerticalAlignment="Center">
            <TextBlock TextTrimming="CharacterEllipsis" Text="{Binding Title}"/>
        </StackPanel>
    </DataTemplate>
</xcad:DockingManager.DocumentHeaderTemplate>

<xcad:DockingManager.LayoutItemContainerStyleSelector>

    <wpf:PaneStyleSelector>
        <wpf:PaneStyleSelector.DocumentStyle>
            <Style TargetType="{x:Type xcad:LayoutItem}">
                <Setter Property="Margin" Value="1000"/>
                <Setter Property="Title" Value="{Binding Model.Title}"/>
                <Setter Property="CanClose" Value="{Binding Model.CanClose}"/>
                <Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}"/>
                <Setter Property="ContentId" Value="{Binding Model.Title}"/>
            </Style>
        </wpf:PaneStyleSelector.DocumentStyle>

        <wpf:PaneStyleSelector.ToolStyle>
            <Style TargetType="{x:Type xcad:LayoutAnchorableItem}">
                <Setter Property="Title" Value="{Binding Model.Title}"/>
                <Setter Property="Visibility" Value="{Binding Model.IsVisible, ConverterParameter={x:Static Visibility.Hidden}, Converter={StaticResource BoolToVisibilityConverter}, Mode=TwoWay}"/>
                <Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}"/>
                <Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/>
                <Setter Property="ContentId" Value="{Binding Model.Title}"/>
            </Style>
        </wpf:PaneStyleSelector.ToolStyle>
    </wpf:PaneStyleSelector>

</xcad:DockingManager.LayoutItemContainerStyleSelector>

1 个答案:

答案 0 :(得分:0)

this is the default ControlTemplate for the LayoutDocumentTabItem you can see the trigger for the DocumentCloseButton which is set IsMouseOver=true visibility=visible you have to put in your ControlTemplate to always be visible

<Style TargetType="{x:Type avalonDockControls:LayoutDocumentTabItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type avalonDockControls:LayoutDocumentTabItem}">
                <avalonDockControls:DropDownControlArea 
                        DropDownContextMenu="{Binding Root.Manager.DocumentContextMenu}"
                        DropDownContextMenuDataContext="{Binding LayoutItem, RelativeSource={RelativeSource TemplatedParent}}" >
                    <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Border Grid.ColumnSpan="2" Background="Transparent"/>
                            <ContentPresenter Content="{Binding Model, RelativeSource={RelativeSource TemplatedParent}}" 
                                          ContentTemplate="{Binding DocumentHeaderTemplate, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type avalonDock:DockingManager}, Mode=FindAncestor}}"
                                          ContentTemplateSelector="{Binding DocumentHeaderTemplateSelector, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type avalonDock:DockingManager}, Mode=FindAncestor}}"/>
                            <!-- Close button should be moved out to the container style -->
                            <Button x:Name="DocumentCloseButton" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Grid.Column="1" Margin="5,0,0,0" Visibility="Hidden" 
                                    Command="{Binding Path=LayoutItem.CloseCommand, RelativeSource={RelativeSource TemplatedParent}}"
                                    ToolTip="{x:Static avalonDockProperties:Resources.Document_Close}">
                                <Image Source="/Xceed.Wpf.AvalonDock;component/Themes/Generic/Images/PinClose.png"/>
                            </Button>
                        </Grid>
                    </Border>
                </avalonDockControls:DropDownControlArea>
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
                        <Setter Property="Visibility" Value="Visible" TargetName="DocumentCloseButton"  />
                    </DataTrigger>
                    <!-- here is your trigger -->
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Visibility" Value="Visible" TargetName="DocumentCloseButton"  />
                    </Trigger>
                    <DataTrigger Binding="{Binding Path=CanClose}" Value="false">
                        <Setter Property="Visibility" Value="Collapsed" TargetName="DocumentCloseButton"  />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>