xaml代码在这里;
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="Button1" Height="25" Width="100" Content="Click Me" Click="Button1_Click" RenderTransformOrigin="0.5,0.5">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
C#代码在这里;
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Thanks for click");
}
vb.net代码在这里;
Private Sub Button1_Click(sender As Object, e As RoutedEventArgs)
MessageBox.Show("Thanks for click")
End Sub
运行上述代码,然后单击 Button1的中心,然后看到MessageBox显示您感谢您点击
我的问题在这里:
单击 Button1的边缘时,您会看到 Button1 表现出按效果,但MessageBox不会打开! (实际上有时打开有时不打开!)
那么,为什么在单击 Button1的边缘时,MessageBox 有时没有打开?
请注意,我尝试使用 LayoutTransform 而不是 RenderTransform ,但是此问题仍然存在。
答案 0 :(得分:0)
乔恩(Jon)有正确的理由无法打开MessageBox
。正确的原因就是您的按钮的大小,然后超出范围。
您应该重新定义按钮样式,以使按钮可以随处可见。
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<!-- This transparent background is important,
because it helps you to hit the button even when it is scaled to 0.9. -->
<Border x:Name="OuterBorder" Background="Transparent">
<Border x:Name="RootBorder" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter />
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="RootBorder" Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="0.9" ScaleY="0.9" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
请注意,我们在外部添加了透明的Border
,以使按钮始终可以被边缘击中。