【问题标题】:UserControl IsPressed gives nothing backUserControl IsPressed 没有任何回报
【发布时间】:2012-05-08 12:47:41
【问题描述】:

我在 Expression Blend 中创建了 UserControl(Button)

XAML:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Marica.FullClient.Graphics.Node"
x:Name="UserControl" Height="360" Width="500" Margin="40">

<UserControl.Resources>
    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.StrokeThickness)" Storyboard.TargetName="rectangle">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle x:Name="rectangle" Stroke="Red" Fill="{TemplateBinding Background}"/>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="StrokeThickness" TargetName="rectangle" Value="13"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="StrokeThickness" TargetName="rectangle" Value="13"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot">
    <Button Name="NodeButton" Style="{DynamicResource ButtonStyle1}"/>
    <StackPanel Orientation="Vertical">
        <TextBlock
        x:Name="NodeName"
        x:FieldModifier="public"
        Text="Property"
        Margin="8,100,8,8"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        TextWrapping="Wrap" 
        TextAlignment="Center"
        FontFamily="Segoe Print"
        FontWeight="Bold" 
        Foreground="White"
        FontSize="50"/>

        <TextBlock
        x:Name="CategoryName"
        x:FieldModifier="public"
        Text="Property"
        Margin="8"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        TextWrapping="Wrap" 
        TextAlignment="Center"
        FontFamily="Segoe Print"
        FontWeight="Bold" 
        Foreground="White"
        FontSize="25"/>
    </StackPanel>
</Grid>

当我将它悬停在控件上时,笔触宽度变大了,但是当我点击时,什么也没有发生。

【问题讨论】:

    标签: c# .net wpf xaml user-controls


    【解决方案1】:

    我希望在 IsPressed 上你想做缓动动画,但它似乎不起作用。好吧,好消息,你的动画很好,你只需要调整它来做你想做的事情就可以看到它确实在工作。玩转 KeyTime 和 Value

    比如改成:

    <EasingDoubleKeyFrame KeyTime="00:00:02" Value="30" />
    

    你会看到它开始发挥作用/做得更多

    【讨论】:

    • 有没有办法让按钮保持这种状态?因此,单击按钮后的笔画厚度保持不变,即 30 ?
    • 好的,我解决了。我刚刚用 Checkbox 替换了 Button,因为 Button 不是合适的控件:)
    • 抱歉,刚刚看到你的 cmets,看起来一切正常,祝你好运
    猜你喜欢
    • 1970-01-01
    • 2021-08-25
    • 2013-07-16
    • 2022-12-12
    • 2021-11-23
    • 2017-12-20
    • 2016-07-28
    • 2011-02-22
    • 1970-01-01
    相关资源
    最近更新 更多