我有以下几句话:
<Style x:Key="WindowButtonTriggers" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource MainColorBrush}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{StaticResource MainColorBrush}" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="MinimizeButton" TargetType="Button" BasedOn="{StaticResource WindowButtonTriggers}">
<Setter Property="Background" Value="{StaticResource WhiteBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Canvas x:Name="appbar_minus" Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0" Background="Transparent" UseLayoutRounding="False">
<Rectangle Width="24" Height="4" Canvas.Left="12" Canvas.Top="24" Stretch="Fill" Fill="{TemplateBinding Background}" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="CloseButton" TargetType="Button" BasedOn="{StaticResource WindowButtonTriggers}">
<Setter Property="Background" Value="{StaticResource WhiteBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Canvas x:Name="appbar_close" Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0" Background="Transparent">
<Path Width="20" Height="20" Canvas.Left="14" Canvas.Top="14" Stretch="Fill" Fill="{TemplateBinding Background}" Data="F1 M 17,14L 24,21L 30.9999,14.0001L 33.9999,17.0001L 27,24L 33.9999,31L 31,34L 24,27L 17,34L 14,31L 21,24L 14,17L 17,14 Z "/>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当我创建一个样式为"最小化按钮"的按钮时,它工作正常(当鼠标悬停在按钮颜色变化上时),但是当我创建一个样式为"关闭按钮"的按钮时,它不起作用,我不明白为什么因为它们几乎是相同的样式。
任何帮助都非常感谢
编辑
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<my:Menu Grid.Column="0" BorderThickness="0,0,1,0" BorderBrush="#FF777777"/>
<WrapPanel Grid.Column="1" VerticalAlignment="Center" Margin="4,0,0,0">
<Button Style="{StaticResource MinimizeButton}" />
<Button Style="{StaticResource CloseButton}" Command="Close" />
</WrapPanel>
</Grid>
它不起作用的原因是未定义"关闭"命令和CanExcecute,然后将按钮上的IsEnabled属性设置为false。MSDN 指出
在任何给定对象上,命令不一定有实现;在许多情况下,命令的实现是应用程序编写者的责任。
下面是一篇关于代码项目的好文章,可帮助您开始使用 WPF 命令。