简体   繁体   English

设置按钮背景图像WPF

[英]Setting Button background Image wpf

I want to design a button with 2 background images(first one when enabled , second when disabled). 我想设计一个带有2个背景图片的按钮(第一个启用时,第二个禁用时)。 As shown below. 如下所示。 How do i do it in button style ? 我该如何以按钮样式进行操作?

tried something like this . 尝试过这样的事情。 But Content is not a property here. 但是Content在这里不是属性。

   <Style TargetType="{x:Type Button}"  x:Key="LinkButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="Overlay" CornerRadius="2">
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="True">
                           <!--<Setter TargetName="Overlay" Property="Content" Value="Red"/>-->
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

在此处输入图片说明

if you want to do it in xaml then have two images inside the button using a canvas. 如果要在xaml中执行此操作,则使用画布在按钮内有两个图像。 Using edit style for trigger change the visibility of the images you need inside the button. 使用触发的编辑样式可以更改按钮内所需图像的可见性。

i think you can get the point. 我认为您可以理解。 Mark useful if it is really useful thank you 如果确实有用,请标记为有用,谢谢

You may do it with help of Style and Converter like this: 您可以像下面这样在Style和Converter的帮助下完成此操作:

Opacity converter: 不透明度转换器:

using System;
using System.Globalization;
using System.Windows.Data;

namespace EnableButton
{
    public class OpacityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool) value ? 1 : 0.5;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Your Xaml: 您的Xaml:

<Window x:Class="EnableButton.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:EnableButton"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:OpacityConverter x:Key="OpacityConverter"/>
        <Style x:Key="DisableIcon" TargetType="Image">
            <Setter Property="Opacity" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsEnabled, Converter={StaticResource OpacityConverter}}"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Height="24" IsEnabled="False">
            <Image Style="{StaticResource DisableIcon}" Source="Open_22.png"/>
        </Button>
    </StackPanel>
</Window>

Now if you change IsEnable property of the button you will see the result. 现在,如果您更改按钮的IsEnable属性,您将看到结果。 I change Opacity property of the Image from 100% to 50% when the parent button becomes disabled in OpacityConverter so you may fix this if you want another digits. 当在OpacityConverter中禁用父按钮时,我将图像的 不透明度属性从100%更改为50%,因此,如果需要其他数字,可以修复此问题。

Update 更新资料

I thought a little about your question. 我想了一下你的问题。 I think if the button becomes disabled it would be nice not only to change the Opacity of the image but also to convert an images picture into monochromic. 我认为,如果禁用该按钮,不仅可以更改图像的不透明度 ,还可以将图像转换为单色图像。 So I found this solution (add reference to Microsoft.Expression.Effects.dll and add xmlns:ee="http://schemas.microsoft.com/expression/2010/effects" into the form before trying): 因此,我找到了该解决方案(尝试之前添加对Microsoft.Expression.Effects.dll的引用,并将xmlns:ee =“ http://schemas.microsoft.com/expression/2010/effects”添加到表单中):

<Window x:Class="EnableButton.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:EnableButton"

        xmlns:ee="http://schemas.microsoft.com/expression/2010/effects"

        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <Style x:Key="DisableImageStyle" TargetType="Image">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsEnabled}" Value="False">
                    <Setter Property="Opacity" Value="0.4"/>
                    <Setter Property="Effect">
                        <Setter.Value>
                            <ee:EmbossedEffect/>
                            <!--This effect is also looks nice-->
                            <!--<ee:MonochromeEffect/>-->
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
    <StackPanel>

        <Button Height="40" IsEnabled="False">
            <Image Style="{StaticResource DisableImageStyle}" Source="Open_22.png"/>
        </Button>

    </StackPanel>
</Window>

Notice that I don't use any converters now! 请注意,我现在不使用任何转换器!

You can use DataTrigger for change background image of button. 您可以使用DataTrigger更改按钮的背景图像。 below is the link may help. 以下是该链接可能会有所帮助。

WPF Change button background image when clicked 单击WPF更改按钮背景图像

I'm not sure how you are setting the style of your button but if you are able to put both images into the style then you can change the visibility when IsEnabled is changed. 我不确定如何设置按钮的样式,但是如果能够将两个图像都放入样式中,则可以在更改IsEnabled时更改可见性。

   <Style TargetType="{x:Type Button}"  x:Key="LinkButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="Overlay" CornerRadius="2">
                    <Image x:Name="Enabled" Source="{StaticResource EnabledImg}"/>
                    <Image x:Name="Disabled" Source="{StaticResource DisabledImg}" Visibility="Collapsed"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                       <Setter TargetName="Disabled" Property="Visibility" Value="Visible"/>
                       <Setter TargetName="Enabled" Property="Visibility" Value="Collapsed"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM