Change the background image of a circle button on a click in WPF

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 to change the background image (Images / light-on.jpg) when I click? Thank!

0
source share
2 answers

Wow! Here you have been given several difficult answers ... you all do too much work! This question has a very simple solution. First, enable this ControlTemplateway it should be:

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

Now you can add a very simple Styleimage modification to do:

<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>
+5
source

, , , , , datatrigger . :

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;
    }
}

. 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>
0

All Articles