【问题标题】:How to disable button click effect in Silverlight for WP7?如何在 Silverlight for WP7 中禁用按钮点击效果?
【发布时间】:2020-07-03 07:58:21
【问题描述】:

我正在使用 Silverlight+XNA 创建一个 WP7 应用程序。在那里,我使用图像作为按钮(Silverlight 元素)的背景,每当我按下按钮时,都会出现白色背景。我怎样才能摆脱它?它似乎是按钮的默认属性。

【问题讨论】:

    标签: silverlight windows-phone-7


    【解决方案1】:

    将按钮的 ClickMode 设置为 Press 将禁用效果

    <Style TargetType="Button">
    <Setter Property="ClickMode" Value="Press"/>
    </Style>
    

    【讨论】:

      【解决方案2】:

      这个问题有一些现有的线程:

      1. check this
      2. or this

      我按照@thongaduka 建议的方式实现了这一点。检查下面的完整实现。

      <StackPanel Height="auto" HorizontalAlignment="Left" Margin="0,0,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="auto" >
          <StackPanel.Resources>
              <Style x:Key="ButtonTemplate_ok" TargetType="Button">
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="Button">
                              <Grid>
                                  <VisualStateManager.VisualStateGroups>
                                      <VisualStateGroup x:Name="CommonStates">
                                          <VisualState x:Name="Normal"/>
                                          <VisualState x:Name="MouseOver">
                                              <Storyboard>
                                                  <ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00" Storyboard.TargetProperty="Background" Storyboard.TargetName="hoverbutton">
                                                      <DiscreteObjectKeyFrame KeyTime="00:00:00">
                                                          <DiscreteObjectKeyFrame.Value>
                                                              <ImageBrush ImageSource="/StickGameSlX;component/Images/buttons/ok_pressed.png"/>
                                                          </DiscreteObjectKeyFrame.Value>
                                                      </DiscreteObjectKeyFrame>
                                                  </ObjectAnimationUsingKeyFrames>
                                              </Storyboard>
                                          </VisualState>
                                      </VisualStateGroup>
                                      <VisualStateGroup x:Name="FocusStates">
                                          <VisualState x:Name="Focused"/>
                                          <VisualState x:Name="Unfocused"/>
                                      </VisualStateGroup>
                                  </VisualStateManager.VisualStateGroups>
                                  <Border x:Name="hoverbutton">
                                      <Border.Background>
                                          <ImageBrush ImageSource="/StickGameSlX;component/Images/buttons/ok_unpressed.png"/>
                                      </Border.Background>
                                  </Border>
                              </Grid>
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
          </StackPanel.Resources>
      
          <!--LayoutRoot is the root grid where all page content is placed-->
          <Grid x:Name="LayoutRoot" >
                 <Button Style="{StaticResource ButtonTemplate_ok}"  Content="OK" HorizontalAlignment="Center" Margin="546,296,198,130" Name="button1" VerticalAlignment="Center" Click="button1_Click" Width="57" Height="53" BorderBrush="White" Foreground="Black"/>       
          </Grid>
      </StackPanel>
      

      【讨论】:

        【解决方案3】:

        你必须为 button 创建一个样式。更改每个视觉状态的按钮属性。喜欢为不同状态设置背景图像属性,如按下、正常等。并为您的按钮应用该样式。 Microsoft Expression Blend 将帮助您通过几个简单的步骤做到这一点。

        【讨论】:

        • 感谢您的回复。但是我在 Visual Studio 本身中没有这个选项吗?
        • 如果你擅长 xaml 编码,你可以在 VS itslef 中完成。 Belnd 是这类任务的工具。
        【解决方案4】:

        您应该使用表达式 blend 来编辑按钮的样式。例如,当您的按钮处于按下状态时,您可以编辑背景或边框画笔。

        【讨论】:

        • 感谢您的回复。但是我在 Visual Studio 本身中没有这个选项吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2011-11-02
        • 1970-01-01
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        相关资源
        最近更新 更多