单击鼠标悬停时可见的WPF按钮

时间:2016-10-19 21:19:36

标签: c# wpf

此处的按钮仅在鼠标悬停在页面上时可见。我试图通过我的代码测试(单击)按钮并失败。我尝试过像悬停然后点击一样的选项,但也不起作用。 有关信息: 此按钮位于StackPanel(例如A)内,该按钮具有与按钮相同的属性(仅在鼠标悬停后可见)。此网格再次嵌入到可见的网格B中。

的Xaml:

<StackPanel Grid.Row="0" Grid.Column="1" Name="CenterButtons" Margin="10,5" VerticalAlignment="Center" Orientation="Horizontal">
                    <StackPanel.Style>
                        <Style TargetType="{x:Type StackPanel}" >
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=CenterButtons, Path=(Grid.Row)}" Value="0">
                                    <Setter Property="HorizontalAlignment" Value="Center" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=CenterButtons, Path=(Grid.Row)}" Value="1">
                                    <Setter Property="HorizontalAlignment" Value="Left" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <Button Name="DeletePageButton" Command="{Binding MarkForDeletionCommand}" Style="{StaticResource NoBorderVectorButtonStyle}"
                                ToolTip="{x:Static resx:DocPdfPages.ToolTipMarkForDeletion}"
                                controls:AdditionalAttachedProperties.GeometryData="{DynamicResource DeleteGlyph}"/>
                    <Button Name="RotateCounterClockwiseButton" Command="{Binding RotateLeftCommand}"
                            Style="{StaticResource NoBorderVectorButtonStyle}"
                            ToolTip="{x:Static resx:DocPdfPages.ToolTipRotatePageLeft}"
                            controls:AdditionalAttachedProperties.GeometryData="{DynamicResource RotateGlyph}">
                        <Button.LayoutTransform>
                            <TransformGroup>
                                <ScaleTransform ScaleX="-1" />
                            </TransformGroup>
                        </Button.LayoutTransform>
                    </Button>
</StackPanel>

1 个答案:

答案 0 :(得分:0)

使用Command而不是Click处理程序的一个好处是,您可以从代码手动调用Command,而无需UI交互。这有助于测试您的场景。

对于Command,您会发现DataContext。然后你可以像这样调用它:

        ICommand deletionCommand = DataContext.MarkForDeletionCommand;
        if (deletionCommand.CanExecute(null))
            deletionCommand.Execute(null);

您的命令不接受任何参数,因此您应该通过为参数提供null来获得正确的行为。

相关问题