在 WPF 中单击时更改圆形按钮背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20558519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:11:52  来源:igfitidea点击:

Change circle button background image on click in WPF

c#wpfwindows-phone-8

提问by 5fox

I have a circle button below

我下面有一个圆圈按钮

<Button  x:Name="btnLight" Width="72" Height="72" Content="" Margin="180,0,372,94" VerticalAlignment="Bottom" d:LayoutOverrides="VerticalAlignment">
            <Button.Template>
                <ControlTemplate>
                    <Grid>
                        <Ellipse>
                            <Ellipse.Fill>
                                <ImageBrush ImageSource="Images/light-off.jpg"/>
                            </Ellipse.Fill>
                        </Ellipse>
                    </Grid>
                </ControlTemplate>
            </Button.Template>
    </Button>

How do I change the background image (Images/light-on.jpg) when I click it? Thank you!

单击时如何更改背景图像 (Images/light-on.jpg)?谢谢!

回答by Sheridan

Wow! You have been given some complicated answers here... you're all doing too much work!!! This question has a really simple solution. First, let's sort out this ControlTemplatethe way it shouldbe:

哇!你在这里得到了一些复杂的答案......你们都做了太多的工作!!!这个问题有一个非常简单的解决方案。首先,让我们按照ControlTemplate应该的方式来解决这个问题:

<Button x:Name="btnLight" Width="72" Height="72" Content="" Margin="180,0,372,94" 
    VerticalAlignment="Bottom">
    <Button.Template>
        <ControlTemplate>
            <Ellipse Name="Ellipse" Fill="{TemplateBinding Background}" />
        </ControlTemplate>
    </Button.Template>
</Button>

Now, you can add a really simple Styleto perform your image change:

现在,您可以添加一个非常简单的Style来执行您的图像更改:

<Style TargetType="{x:Type Button}">
    <Setter Property="Button.Background">
        <Setter.Value>
            <ImageBrush ImageSource="Images/Add_16.png" />
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Button.IsPressed" Value="True">
            <Setter Property="Button.Background">
                <Setter.Value>
                    <ImageBrush ImageSource="Images/Copy_16.png" />
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

回答by Mark Feldman

To do it properly you need to create a view model that contains a handler for the button to call when it's pressed and a boolean property you can use for a datatrigger to change the image. Start with the view model:

要正确执行此操作,您需要创建一个视图模型,其中包含按钮按下时要调用的处理程序和可用于数据触发器以更改图像的布尔属性。从视图模型开始:

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public RelayCommand OnClickedCommand { get; private set; }

    private bool _ImageChanged;
    public bool ImageChanged
    {
        get { return this._ImageChanged; }
        private set {
            this._ImageChanged = value;
            OnPropertyChanged("ImageChanged");
        }
    }

    public ViewModel()
    {
        this.OnClickedCommand = new RelayCommand(param => OnClicked());
    }

    private void OnClicked()
    {
        this.ImageChanged = true;
    }
}

Now create an instance of it and set it as your button's data context. Your button XAML should looks something like this:

现在创建它的一个实例并将其设置为按钮的数据上下文。您的按钮 XAML 应如下所示:

<Button x:Name="btnLight" Margin="148,0,372,63" VerticalAlignment="Bottom" Command="{Binding OnClickedCommand}" Height="69">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid>
                                <Ellipse>
                                    <Ellipse.Fill>
                                        <ImageBrush ImageSource="image1.png"/>
                                    </Ellipse.Fill>
                                </Ellipse>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ImageChanged}" Value="True">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Grid>
                                        <Ellipse>
                                            <Ellipse.Fill>
                                                <ImageBrush ImageSource="image2.png"/>
                                            </Ellipse.Fill>
                                        </Ellipse>
                                    </Grid>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>