【问题标题】:Trigger properties not working in WPF button触发器属性在 WPF 按钮中不起作用
【发布时间】:2014-05-06 21:16:13
【问题描述】:

我正在尝试在 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>

这是我的按钮的代码,它们都不起作用。:

 <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>

【问题讨论】:

  • 请取出命令并检查一旦我用 out 命令检查了它的工作正常。我不熟悉 Prism,我不知道什么是 shell 和所有。我认为你需要做一些 w.r.t 命令。让我知道它现在是如何工作的。
  • @Dineshreddybandi StaticShell 是 WPF shell 集成库的一部分。当我从按钮中删除命令时,触发器确实有效。非常感谢您的建议。我相信它一定会发生,因为我还没有为命令添加 c# 代码。我不认为它对触发器很重要,我想它确实如此。 WinForms 和 WPF 之间的一些差异让我大吃一惊。我需要习惯很多东西。不过你的建议奏效了,谢谢。如果您想将其发布为答案,您可以,我会选择它。
  • @user1632018 看看stackoverflow.com/questions/11000830/… 上的答案是否有效。

标签: c# .net wpf xaml


【解决方案1】:

我们需要定义一个 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>

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

【讨论】:

    【解决方案2】:

    请取出命令并在我用 out 命令检查它的工作正常后检查。我不熟悉 Prism,我不知道什么是 shell 和所有。我认为你需要做一些 w.r.t 命令。让我知道它现在是如何工作的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-11
      • 2016-12-05
      • 2012-08-21
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多