C# 触发器属性在WPF按钮中不起作用

C# 触发器属性在WPF按钮中不起作用,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我试图在WPF中为我的表单创建最小化、最大化和关闭按钮。我可以很好地创建按钮,但当我悬停或单击按钮时,它们不会改变颜色,即使XAML已设置为这样做。有人能看看我做错了什么吗?谢谢 <Window.Resources> <Color x:Key="HighlightColor">#FF3F3F41</Color> <Color x:Key="BlueColor">#FF007ACC</Color> <Colo

我试图在WPF中为我的表单创建最小化、最大化和关闭按钮。我可以很好地创建按钮,但当我悬停或单击按钮时,它们不会改变颜色,即使XAML已设置为这样做。有人能看看我做错了什么吗?谢谢

<Window.Resources>
    <Color x:Key="HighlightColor">#FF3F3F41</Color>
    <Color x:Key="BlueColor">#FF007ACC</Color>
    <Color x:Key="ForegroundColor">#FFF4F4F5</Color>

    <SolidColorBrush x:Key="HighlightColorBrush" Color="{StaticResource HighlightColor}"/>
    <SolidColorBrush x:Key="BlueColorBrush" Color="{StaticResource BlueColor}"/>
    <SolidColorBrush x:Key="ForegroundColorBrush" Color="{StaticResource ForegroundColor}"/>

    <Style x:Key="WindowButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Foreground" Value="{DynamicResource ForegroundColorBrush}" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Padding" Value="1" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter x:Name="contentPresenter"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                      Margin="{TemplateBinding Padding}"
                      RecognizesAccessKey="True" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="{StaticResource HighlightColorBrush}" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{DynamicResource BlueColorBrush}" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="contentPresenter" Property="Opacity" Value=".5" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

#FF3F3F41
#FF007ACC
#FFF4
这是我的按钮的代码,它们都不起作用

 <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" shell:WindowChrome.IsHitTestVisibleInChrome="True" Grid.Row="0">
            <Button Command="{Binding Source={x:Static shell:SystemCommands.MinimizeWindowCommand}}" ToolTip="minimize" Style="{StaticResource WindowButtonStyle}">
                <Button.Content>
                    <Grid Width="30" Height="25" RenderTransform="1,0,0,1,0,1">
                        <Path Data="M0,6 L8,6 Z" Width="8" Height="7" VerticalAlignment="Center" HorizontalAlignment="Center"
                                                        Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="2"  />
                    </Grid>
                </Button.Content>
            </Button>
            <Grid Margin="1,0,1,0">
                <Button x:Name="Restore" Command="{Binding Source={x:Static shell:SystemCommands.RestoreWindowCommand}}" ToolTip="restore" Visibility="Collapsed" Style="{StaticResource WindowButtonStyle}">
                    <Button.Content>
                        <Grid Width="30" Height="25" UseLayoutRounding="True" RenderTransform="1,0,0,1,.5,.5">
                            <Path Data="M2,0 L8,0 L8,6 M0,3 L6,3 M0,2 L6,2 L6,8 L0,8 Z" Width="8" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"
                                                            Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="1"  />
                        </Grid>
                    </Button.Content>
                </Button>
                <Button x:Name="Maximize" Command="{Binding Source={x:Static shell:SystemCommands.MaximizeWindowCommand}}" ToolTip="maximize" Style="{StaticResource WindowButtonStyle}">
                    <Button.Content>
                        <Grid Width="31" Height="25">
                            <Path Data="M0,1 L9,1 L9,8 L0,8 Z" Width="9" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"
                                                            Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="2"  />
                        </Grid>
                    </Button.Content>
                </Button>
            </Grid>
            <Button Command="{Binding Source={x:Static shell:SystemCommands.CloseWindowCommand}}" ToolTip="close"  Style="{StaticResource WindowButtonStyle}">
                <Button.Content>
                    <Grid Width="30" Height="25" RenderTransform="1,0,0,1,0,1">
                        <Path Data="M0,0 L8,7 M8,0 L0,7 Z" Width="8" Height="7" VerticalAlignment="Center" HorizontalAlignment="Center"
                                                        Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="1.5"  />
                    </Grid>
                </Button.Content>
            </Button>
        </StackPanel>


请取出该命令,并在我检查完它后进行检查。我不熟悉Prism,也不知道shell是什么。我想你需要使用w.r.t命令。让我知道它现在是如何工作的

我们需要定义一个CommandBindings,它将命令与其处理程序相关联。下面是xaml和代码隐藏。试着让我知道这是否对你有帮助

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            CommandBinding cmdBinding = new CommandBinding (Microsoft.Windows.Shell.SystemCommands.CloseWindowCommand,
                CloseCommandHandler);
            this.CommandBindings.Add(cmdBinding);
        }

        private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Parameter != null)
            {
                (e.Parameter as Window).Close();
            }
        }
    }

<Button x:Name="btn3" Command="shell:SystemCommands.CloseWindowCommand" CommandParameter="{Binding ElementName=myWindow}" ToolTip="close"  Style="{StaticResource WindowButtonStyle}">
            <Button.Content>
                <Grid Width="30" Height="25" RenderTransform="1,0,0,1,0,1">
                    <Path Data="M0,0 L8,7 M8,0 L0,7 Z" Width="8" Height="7" VerticalAlignment="Center" HorizontalAlignment="Center"
                                                        Stroke="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" StrokeThickness="1.5"  />
                </Grid>
            </Button.Content>
        </Button>
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
CommandBinding cmdBinding=新建CommandBinding(Microsoft.Windows.Shell.SystemCommands.CloseWindowCommand,
CloseCommandHandler);
this.CommandBindings.Add(cmdBinding);
}
私有void CloseCommandHandler(对象发送方,ExecutedRoutedEventArgs e)
{
如果(例如参数!=null)
{
(e.参数作为窗口)。关闭();
}
}
}

注意:Xaml命令参数中的myWindow是窗口的名称

请拿出命令,检查一下,一旦我检查了它的工作与out命令。我不熟悉棱镜,我不知道什么是外壳和所有。我想你需要做一些w.r.t命令。让我知道它现在是如何工作的。@Dineshredybandi StaticShell是WPF shell集成库的一部分。当我从按钮中删除命令时,触发器实际上起作用。非常感谢你的建议。我相信这一定是因为我没有为命令添加c#代码。我不认为这对触发器有什么关系,我想是的。WinForms和WPF之间的一些差异让我很反感。我需要适应的东西很多。不过你的建议奏效了,谢谢。如果你想把它作为一个答案发布,你可以,我会选择它。@user1632018查看上面的答案是否有效。