Programmatically changing button icon in WPF

前端 未结 4 1237
忘掉有多难
忘掉有多难 2020-12-01 12:50

I currently have a button, which has an icon/image on it. I have configured the button and image in XAML:

4条回答
  •  时光说笑
    2020-12-01 13:10

    Use a DataTrigger (edit) in the Image's Style (/edit) on the change condition:

    
    

    The myCondition variable would then be a boolean property in your ViewModel (or, more general, the Control's DataContext), something like

    public bool myCondition { get { return ([whatever that condition might be]); } }
    

    This may also include a setter and could as well be a simple auto property. As with the other MVVM answer, it will rely on the ViewModel to implement INotifyPropertyChanged.

    The nice thing is, once the condition is no longer fulfilled, the DataTrigger will automatically set the Source property back to its original value.

    Disclaimer: I have no way to test that right now, so take this with a grain of salt and probably some debugging effort...

提交回复
热议问题