在WPF中单击按钮的仅图标不能完全按下按钮

时间:2019-04-03 10:57:24

标签: c# wpf button

我正在尝试使用c#wpf制作服装按钮。我使用了材料设计主题在按钮内插入图标,但是当我运行程序时,仅单击按钮内的图标不能单击整个按钮。我该怎么做才能解决此问题?

我这样做是为了实现按钮样式:

<Application.Resources>

           <Style x:Key="MyButton" TargetType="{x:Type Button}">
           <Setter Property="OverridesDefaultStyle" Value="True" />
           <Setter Property="Cursor" Value="Hand" />
           <Setter Property="Template">
               <Setter.Value>
                   <ControlTemplate TargetType="{x:Type Button}">
                       <Border Name="border" BorderThickness="0" Background="{TemplateBinding Background}">
                           <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />

                       </Border>
                       <ControlTemplate.Triggers>
                           <Trigger Property="IsMouseOver" Value="True" >
                               <Setter Property="Opacity" Value="0.8"  />

                           </Trigger>
                       </ControlTemplate.Triggers>
                   </ControlTemplate>
               </Setter.Value>
           </Setter>
       </Style>

   </Application.Resources>

这是按钮:

 <Button x:Name="btnClose" Style="{StaticResource MyButton}"  Width="30" Height="30" Padding="0" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Gray" Margin="10 0" Click="btnClose_Click">
                        <materialDesign:PackIcon Kind="Power" Height="30" Width="30"></materialDesign:PackIcon>
                    </Button>

1 个答案:

答案 0 :(得分:0)

Background属性设置为Brush,例如将Transparent设置为{x:Null}

<Button x:Name="btnClose" Style="{StaticResource MyButton}"  Width="30" Height="30" Padding="0" 
        Background="Transparent" BorderBrush="{x:Null}" Foreground="Gray" Margin="10 0" Click="btnClose_Click">
    <materialDesign:PackIcon Kind="Power" Height="30" Width="30"></materialDesign:PackIcon>
</Button>