VisualStateManager 似乎不适用于 .NET 4.0 (wpf)

发布于 2024-11-15 17:13:39 字数 2361 浏览 5 评论 0原文

我想覆盖默认按钮样式,以便只有一个图像按钮,没有边框和所有这些东西。

这一切都工作得很好,但是尽管我在 XAML 样式中定义了 VisualStateManager,但无法用鼠标按下该按钮。事实上,它什么也没做。

我做错了什么?

<!-- Button Style -->
<Style x:Key="WinImageButton" TargetType="Button">

    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="Transparent"/>

    <Setter Property="RenderTransform">
        <Setter.Value>
            <TranslateTransform/>
        </Setter.Value>    
    </Setter> 

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ContentControl x:Name="RootElement">
                    <ContentPresenter 
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        RecognizesAccessKey="True">
                    </ContentPresenter>

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup Name="CommonStates">
                            <VisualState Name="Normal" />

                            <VisualState Name="Pressed">
                                <Storyboard>
                                    <DoubleAnimation 
                                        Storyboard.TargetName="RootElement"
                                        Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TranslateTransform.Y)" 
                                                 To="2.0" />
                                    <DoubleAnimation 
                                        Storyboard.TargetName="RootElement"
                                        Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TranslateTransform.X)" 
                                                 To="-2.0"  />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                </ContentControl >
            </ControlTemplate>

        </Setter.Value>
    </Setter>
</Style>

I wanto to override the Default Button Style in order to have only an image button, without a border and all that stuff.

That's all working perfectly, but although I defined the VisualStateManager in my XAML Style, the button can't be pressed with the mouse. In fact, it does nothing.

What do I do wrong?

<!-- Button Style -->
<Style x:Key="WinImageButton" TargetType="Button">

    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Background" Value="Transparent"/>

    <Setter Property="RenderTransform">
        <Setter.Value>
            <TranslateTransform/>
        </Setter.Value>    
    </Setter> 

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ContentControl x:Name="RootElement">
                    <ContentPresenter 
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        RecognizesAccessKey="True">
                    </ContentPresenter>

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup Name="CommonStates">
                            <VisualState Name="Normal" />

                            <VisualState Name="Pressed">
                                <Storyboard>
                                    <DoubleAnimation 
                                        Storyboard.TargetName="RootElement"
                                        Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TranslateTransform.Y)" 
                                                 To="2.0" />
                                    <DoubleAnimation 
                                        Storyboard.TargetName="RootElement"
                                        Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TranslateTransform.X)" 
                                                 To="-2.0"  />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                </ContentControl >
            </ControlTemplate>

        </Setter.Value>
    </Setter>
</Style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅一夜美梦 2024-11-22 17:13:40

您是否尝试过将 TranslateTransition 移动到 ControlTemplate 本身内部? (您可以将其放在模板的资源部分中。)

Have you tried moving the TranslateTransition to inside the ControlTemplate itself? (You can put it in the resources section of the template.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文