【问题标题】:Disable default button click effect禁用默认按钮点击效果
【发布时间】:2014-01-31 14:27:52
【问题描述】:

我以编程方式创建了按钮:

Button button = new Button();
ImageBrush background = new ImageBrush();
background.ImageSource = new System.Windows.Media.Imaging.BitmapImage(new Uri(@"Assets/Image1.png", UriKind.Relative));
button.Background = background; 
button.Click += button_Click;

现在,当我单击该按钮时,我需要删除默认的单击效果并添加我自己的图像,这样当我单击该按钮时,应该显示我添加的图像。

编辑

上面有 2 个赞成票的作品,但它替换了背景图像,但我希望显示这两个图像,只有 onclick 它应该将前景图像添加到我的按钮。

我怎样才能做到这一点?

【问题讨论】:

    标签: c# windows silverlight windows-8 windows-phone-8


    【解决方案1】:

    你需要玩弄按钮的视觉状态,你可以借助样式来做到这一点

    看看下面的样式

    <Style x:Key="style_ColorButton" TargetType="Button">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
            <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
            <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
            <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>
            <Setter Property="Padding" Value="10,3,10,5"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid Background="Transparent">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <ImageBrush Stretch="Fill" ImageSource="/Images/ColorClicked.png"/>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0">
                                <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    在 VisualState 上面的样式中“Pressed”处理您的要求,我已经在那里提供了一个图像源。

    将此样式与按钮一起使用。

    <Button Height="40" Width="40" BorderThickness="0" Name="btnAcceptCrop" Click="btnAcceptCrop_Click" Style="{StaticResource style_ColorButton}" Background="Black" ForeGround="White"/>
    

    在代码中设置样式

    btnAcceptCrop.Style = (Style)App.Current.Resources["style_ColorButton"];
    

    在页面中声明样式

    在页面中的所有命名空间声明下方

    只做一个标签

    <phone:PhoneApplicationPage.Resources>
    </phone:PhoneApplicationPage.Resources>
    

    并在其中声明你的风格。

    希望这会有所帮助。

    【讨论】:

    • 我怎样才能以编程方式完成,你能帮帮我吗?
    • 样式在按钮按下动作时自动改变图像,你到底想用代码写什么?改变风格?或制作一个?请根据您的要求添加更多代码或快照。
    • 由于我以编程方式创建了按钮,当我单击该按钮时,我需要指示用户他已单击该按钮,因为我不想要默认颜色,我希望显示图像最重要的是,我的按钮已经有了背景图片
    • 这种风格非常适合您的要求,只要用代码管理,然后检查我所做的编辑。
    • 好的,我是 Windows 应用开发新手,我需要在哪里添加样式?你写的
    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    相关资源
    最近更新 更多