rideronthestorm
rideronthestorm

Reputation: 747

WPF DataGrid changing a button text depending on a value

I would like to have the text on the button changed when the value of the variable is changing. For example: value of the var is 0 (turned off), so button should display text "turn on"; if the var value is 1 the button should display "turn off". I would like also to display an image in another column depending on the value of this variable. How can I do it?

Upvotes: 0

Views: 1924

Answers (1)

Joe White
Joe White

Reputation: 97718

If you're using MVVM, you can add another property on your ViewModel that returns either "turn on" or "turn off", implement INotifyPropertyChanged to tell the binding system when its value changes, and bind your button's Content to it:

<Button Content="{Binding ButtonText}"/>

If you're not using MVVM, you'll need to write a value converter. But you'll still need your backing object to implement INotifyPropertyChanged -- otherwise the button will never know that the Boolean value changed, so it will never update its text. So I would probably recommend the ViewModel approach instead of the value converter.

Upvotes: 3

Related Questions