简体   繁体   English

为什么在图片更改之间绑定以单击它不起作用

[英]Why Binding Between Picture Change To Click on It Not Working

I want when I press on bulb button every time picture will change.我希望每次按下灯泡按钮时图片都会改变。 I tried but without success.我试过但没有成功。 My code doesn't work, please tell my why, thanks.我的代码不起作用,请告诉我的原因,谢谢。

Here is the UserControl in XAML:这是 XAML 中的 UserControl:

<UserControl x:Class="PL_Wpf.CustomControls.Bulb"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"           
             mc:Ignorable="d" 
             >
    <Grid>                   
        <Button x:Name="BulbButton" Height="60" Width="40" Click="BulbButton_Click" >        
            <Button.Template>
                <ControlTemplate>
                    <Image  Source="../Pics/bulb_off.jpg" Width="40" Height="60"  >
                        <Image.Style>
                            <Style TargetType="{x:Type Image}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=Bulb, Path=OnOff}" Value="True">
                                        <Setter Property="Source" Value="../Pics/bulb_on.jpg" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ElementName=Bulb, Path=OnOff}" Value="False">
                                        <Setter Property="Source" Value="../Pics/bulb_off.jpg" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </Grid>
</UserControl>

And here is the code behind:这是背后的代码:

public partial class Bulb : UserControl, INotifyPropertyChanged
{
    public bool OnOff { get; set; }

    public Bulb()
    {
        InitializeComponent();
        DataContext = this;
        OnOff = false;
    }
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }

    public void BulbButton_Click(object sender, RoutedEventArgs e)
    {
        OnOff = (OnOff == false) ? true : false;
    }

    public bool On
    {
        get { return OnOff; }
        set
        {
            OnOff = value;
            OnPropertyChanged(new PropertyChangedEventArgs("On"));
        }
    }
}

The think that you want to achieve with your control can be simply done by modifying the control template of a checkbox.你想用你的控件实现的想法可以通过修改一个复选框的控件模板来简单地完成。

Just add this code in the window in place of your control:只需在窗口中添加此代码来代替您的控件:

<CheckBox>
        <CheckBox.Template>

            <ControlTemplate TargetType="CheckBox">
                <Grid>
                    <Image  Source="pack://siteoforigin:,,,/Pics/bulb_off.jpg" Width="40" Height="60"  />
                    <Image  Source="pack://siteoforigin:,,,/Pics/bulb_on.jpg" Width="40" Height="60" Name="CheckMark" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="false">
                        <Setter TargetName="CheckMark" Property="Visibility" Value="Hidden"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </CheckBox.Template>
    </CheckBox>

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

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