【问题标题】:WPF app.xaml button after click, not back to normal image点击后WPF app.xaml按钮,不恢复正常图像
【发布时间】:2019-01-27 03:32:43
【问题描述】:

我使用了此代码,但在单击按钮后它不会恢复为默认样式。 我做错了什么?

<Style x:Key="normalImage" TargetType="{x:Type Image}">
            <Setter Property="Source" Value="/SM-Works;component/Images/code_101.png"/>
        </Style>
        <Style x:Key="actionImage" TargetType="{x:Type Image}">
            <Setter Property="Source" Value="/SM-Works;component/Images/code_10b.png"/>
        </Style>
        <Style x:Key="buttonstylesample" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Image x:Name="img" Style="{DynamicResource normalImage}"/>
                            <Border
                                SnapsToDevicePixels="True"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}">
                                <ContentPresenter
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderThickness" Value="0"/>
                                <Setter Property="Style" TargetName="img" Value="{DynamicResource actionImage}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Style" TargetName="img" Value="{DynamicResource actionImage}"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter Property="Style" TargetName="img" Value="{DynamicResource actionImage}"/>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

MouseOver,IsPressed 被激活,但是按下按钮后,按钮图像没有恢复正常。

【问题讨论】:

    标签: wpf button properties styles


    【解决方案1】:

    由于以下触发器而发生:

    <Trigger Property="IsKeyboardFocused" Value="True">
        <Setter Property="Style" TargetName="img" Value="{DynamicResource actionImage}"/>
    </Trigger>
    

    所以发生的情况是,当您按下按钮时,键盘焦点也会转到按钮上,因此当您释放鼠标时,您仍然会应用 actionImage 样式。您可以通过将焦点移出按钮来验证此效果,您会看到img 上的样式将重置为normalImage 样式。

    【讨论】:

      猜你喜欢
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      相关资源
      最近更新 更多