I cannot for the life of me figure out how to change a ToggleButton image when clicked. I have looked up countless examples and they are all outdated and no longer work. Or if they do I cannot get them to work. Does anyone have an up to date example I could look at or any suggestions?
I tried doing it in the code behind first. The example I found used a BitmapImage but this is not possible anymore as the BeginInit method cant be used due to security reasons.
Next I tried numerous style triggers but I get way to many compile errors even when they are directly copied and modified to fit the correct parameters. So I am stuck. I cant figure out how to use an EventTrigger to do it nor do any older examples seem to work. Anyone have any ideas?
Why not something like:
Where you have a boolean to visibility converter that can accept a parameter to invert the bool.
Edit:
You’ll need to define a converter so that it can convert the
bool?from theIsCheckedproperty to aVisibilityenum. That’s what all the binding code does. There is a basic implementation here that will convert toVisibility.Visiblewhen true andVisibility.Collapsedwhen false. You need to add a check for theparameterso that it inverts the visibility whenInvertis passed (to toggle between two images).The other way to do this is to define images in the style and use the visual states for
CheckedandUncheckedto flip flop the images. You can apply a style to multiple buttons but it’s hard to vary the images per-button (what my solution does).This is how you set up a Resource
XAML
Then use the converter here BooleanToVisibilityConverter