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

5fo*_*fox 0 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>
Run Code Online (Sandbox Code Playgroud)

点击它后,如何更改背景图像(Images/light-on.jpg)?谢谢!

She*_*dan 6

哇!你在这里得到了一些复杂的答案......你们都做了太多的工作!这个问题有一个非常简单的解决方案 首先,让我们理清这个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>
Run Code Online (Sandbox Code Playgroud)

现在,您可以添加一个非常简单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>
Run Code Online (Sandbox Code Playgroud)