IsPressed触发器在False时起作用,在True时不起作用

时间:2014-09-17 13:16:49

标签: wpf xaml button triggers

我的应用程序中有多种样式。以下是这个问题的内容:

<Style x:Key="RoundCornerSmart" TargetType="{x:Type vk:SmartButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type vk:SmartButton}">
                <Border CornerRadius="8" BorderBrush="#006AB6" BorderThickness="1" Name="border">
                    <Grid x:Name="grid">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Grid>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" TargetName="border">
                            <Setter.Value>
                                LightGray
                            </Setter.Value>
                        </Setter>
                    </Trigger>

                    <Trigger Property="IsPressed" Value="False">
                        <Setter Property="Background" TargetName="border">
                            <Setter.Value>
                                White
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="FontSize" Value="18" />
    <Setter Property="Height" Value="62" />
    <Setter Property="Width" Value="62" />
    <Setter Property="Margin" Value="10" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="BorderBrush" Value="Black" />
</Style>

SmartButton是一个扩展Button类的类,它是一个Button用户控件。

 <vk:SmartButton HoldCommand="{Binding Path=ClickCommand}" EnableClickHold="True" MillisecondsToWait="1000" x:Name="key_U" Content="U" Grid.Column="8" Style="{DynamicResource RoundCornerSmart}"/>

当没有按下智能按钮时 - 它的背景是白色的,这是它应该是的样子。但是,当按下它时,背景不会改变。它还是白色的。我究竟做错了什么?为什么没有触发IsPressed?

编辑:

当我评论OnPreviewMouseLeftButtonDown方法时,它可以工作。

以下是该方法中发生的情况:

if (EnableClickHold)
        {
            Timer = new DispatcherTimer(DispatcherPriority.Normal, this.Dispatcher)
            {
                Interval = TimeSpan.FromMilliseconds(MillisecondsToWait)
            };
            Timer.Tick += Timer_Tick;
            Timer.IsEnabled = true;
            Timer.Start();
            e.Handled = true;
        }

1 个答案:

答案 0 :(得分:1)

问题是:

e.Handled=true; 

OnPreviewMouseLeftButtonDown方法中的那一行。我不知道为什么。当我评论它时,背景设置为LightGrey,它有效。

相关问题