Can the result of a MultiValueConverter be itself converted?

349 views Asked by At

I have a form in my WPF application that I have to add an Edit button to. Right now, the form is always in edit mode and there is no "view mode". However, for various reasons, I need to keep the control in view mode until the user clicks an Edit button, and then hide the edit button and display a Save button in its place.

My form already has a bool DependencyProperty called CanModify, which is true when the user's permissions give them the right to edit the record being displayed in the form. I am adding a new bool DependencyProperty called InEditMode, which will default to false. Hiding and enabling the buttons is pretty straight forward, but I have this ComboBox control that I want to hide if the user can't edit the record, or if they can edit it and the form is in view mode. For that, I need to convert the result of ANDing the two bool properties together and then convert it into a Visibility value.

I've already got a class that implements IValueConverter and converts a bool into a Visibility. I've just written a class that implements IMultiConverter which takes an array of bools and ANDS them together (or ORs them, depending on the value of the parameter).

What I'd like to do is to take the result of the IMultiConverter and put it through the IValueConverter to convert the result into a Visibility. Can I do that? Or would I be better off doing the AND in the code behind to a new DependencyProperty?

Tony

3

There are 3 answers

0
Tony Vitabile On BEST ANSWER

Because you can't chain converters, I added another boolean property to my class. I added methods to the two properties the new one is dependent upon that are called when they change. These recompute the value of the new property. I then used my original boolean to visbility converter to show or hide the control in question.

1
Wallstreet Programmer On

You can't chain converters.

Instead change your MultiValueConverter to take a string parameter, "bool" or "visibility". Depending on the parameter return either a bool or visibility object

3
Peter Karlsson On

You can indeed chain converters, my own example does not yet deal with MultiValueConverters but it shouldn't be too hard to implement a LinkedMultiValueConverter. One of the downsides of this approach is that you would have to recreate any converters you currently have to use the provided attribute.

git://github.com/pmacn/ValueConverters.WP8.git

or

https://github.com/pmacn/ValueConverters.WP8

and then you would do the following

<con:LinkedConverter x:Name="MyFancyLinkedConverter">
    <con:BooleanInversionConverter />
    <con:BooleanToVisibilityConverter />
</con:LinkedConverter>