2013-05-01 10 views
7

पर क्लिक किया है मैंने Button बनाया है और इसकी पृष्ठभूमि Image सेट की है। मुझे क्या चाहिए जब Button क्लिक किया गया है, मैं पृष्ठभूमि Image को अन्यWPF बदलें बटन पृष्ठभूमि छवि जब

से कैसे बदल सकता हूं, मैं इसे कैसे पूरा कर सकता हूं?

यहाँ Button के साथ मेरे कोड है:

<Button x:Name="PopulationReporting" Click="PopulationReporting_Clicked" Width="172" 
     Height="60" Margin="57,170,57,184"> 
    <Button.Background > 
     <ImageBrush ImageSource="images/img-2.png" /> 
    </Button.Background> 
</Button> 

उत्तर

14

आप इस प्रोग्राम के रूप में कर सकते हैं (उदाहरण के here देखें)

या

आप DataTriggers, जहां DataTrigger एक के लिए बाध्य है उपयोग कर सकते हैं bool आपके ViewModel में मूल्य और आपके Button के Style को बदलें। ButtonCommand से जुड़ा हुआ है, इसलिए जब निष्पादित किया गया, Commandimage (isPlaying संपत्ति) की स्थिति को बदल देगा।

XAML:

<Button Height="23" HorizontalAlignment="Left" Margin="70,272,0,0" Name="buttonPlay" VerticalAlignment="Top" Width="75" Command="{Binding PlayCommand}" CommandParameter="{Binding ElementName=ButtonImage, Path=Source}" > 
     <Image Name="ButtonImage"> 
      <Image.Style> 
       <Style TargetType="{x:Type Image}"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding isPlaying}" Value="True"> 
          <Setter Property="Source" Value="Play.png" /> 
         </DataTrigger> 
         <DataTrigger Binding="{Binding isPlaying}" Value="False"> 
          <Setter Property="Source" Value="Stop.png" /> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Image.Style> 
     </Image> 
</Button> 

C#:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new ViewModel(); 
    } 
} 

public class ViewModel : INotifyPropertyChanged 
{ 
    private bool _isPlaying = false; 
    private RelayCommand _playCommand; 

    public ViewModel() 
    { 
     isPlaying = false; 
    } 

    public bool isPlaying 
    { 
     get { return _isPlaying; } 
     set 
     { 
      _isPlaying = value; 
      OnPropertyChanged("isPlaying"); 
     } 
    } 

    public ICommand PlayCommand 
    { 
     get 
     { 
      return _playCommand ?? new RelayCommand((x) => 
      { 
       var buttonType = x.ToString(); 

       if (null != buttonType) 
       { 
        if (buttonType.Contains("Play")) 
        { 
         isPlaying = false; 
        } 
        else if (buttonType.Contains("Stop")) 
        { 
         isPlaying = true; 
        } 
       } 
      }); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

public class RelayCommand : ICommand 
{ 
    private readonly Predicate<object> _canExecute; 
    private readonly Action<object> _execute; 

    public event EventHandler CanExecuteChanged; 

    public RelayCommand(Action<object> execute) : this(execute, null) { } 

    public RelayCommand(Action<object> execute, Predicate<object> canExecute) 
    { 
     _execute = execute; 
     _canExecute = canExecute; 
    } 

    public bool CanExecute(object parameter) 
    { 

     if (_canExecute == null) 
     { 
      return true; 
     } 

     return _canExecute(parameter); 
    } 

    public void Execute(object parameter) 
    { 
     _execute(parameter); 
    } 

    public void RaiseCanExecuteChanged() 
    { 
     if (CanExecuteChanged != null) 
     { 
      CanExecuteChanged(this, EventArgs.Empty); 
     } 
    } 
} 
+1

धन्यवाद @MONCADAD –

संबंधित मुद्दे