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

时间:2013-12-13 03:22:27

标签: c# wpf windows-phone-8

我在

下面有一个圆圈按钮
<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>

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

2 个答案:

答案 0 :(得分:5)

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

的方式理清这个ControlTemplate

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

现在,您可以添加一个非常简单的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>

答案 1 :(得分:0)

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

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>