2

I'm trying to make a pair of TextBlocks toggle (one Visible, the other Collapsed and vice-versa) on a bound boolean.

I could use BooleanToVisibilityConverter for one but can't do a ! for the other so I'm trying to use MVVM Light's UniversalConverter which takes a lambda in the ConverterParameter.

Trouble is, I can't find any examples of the use and my attempts fail.

I've declared it in the XAML resources:

<Window.Resources>
    <ex:UniversalConverter x:Key="UniversalConverter" />
</Window.Resources>

and then added this to the TextBlock:

<TextBlock Visibility="{Binding Path=ShowA, Converter={StaticResource universalConverter}, ConverterParameter='b=>b?Visible:Collapsed'}">A</TextBlock>
<TextBlock Visibility="{Binding Path=ShowA, Converter={StaticResource universalConverter}, ConverterParameter='b=>b?Collapsed:Visible'}">B</TextBlock>

This throws an exception in the XAML design windows: Unknown identifier 'Visible'.

I've also tried b=>b?Visibility.Visible:Visibility.Collapsed which is an example in the UniversalConverter's Convert method comment; that throws Unknown identifier 'Visibility'.

How do I add the references needed to the UniversalConverter?

STiLeTT
  • 1,023
  • 10
  • 23
PeteB
  • 151
  • 1
  • 12

2 Answers2

2

Honestly, I never used the UniversalConverter class from the excellent MVVM Light toolkit framework, so I can't answer your specific question "how to make it work".

But in order to solve your actual issue, you can create your own BooleanToVisibilityConverter that allows inverting the mapping between true/false and Visible/Collapsed. See How do I invert BooleanToVisibilityConverter? for examples.

Personally I really would avoid to use the UniversalConverter for many reasons:

  • undocumented as you now know
  • forces to write programming logic in the XAML, which is not the primary goal of the XAML...
  • ... forces to write C# line of code that cannot be debugged (at least for VS2010 + .Net 4), which means this converter is a wrong approach IMHO
Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176
  • 1
    I have to admit that I don't disagree with Ken. UniversalConverter is something I tried playing with, but I must admit that I never use it. Seriously considering deprecating it and eventually removing it from MVVM Light. – LBugnion Jul 26 '12 at 13:01
  • Well if Laurent "doesn't disagree" then ken must be right. I guess I wanted to use the UniversalConverter because it seemed like a quick win (_marginally_ quicker than writing my own specific converter)... – PeteB Jul 27 '12 at 10:38
0

It seems UniversalConverter has been removed since year 2013

http://blog.galasoft.ch/posts/2013/01/mvvm-light-v4-1-26-change-log/

BL0023.004, Remove UniversalConverter

celeron533
  • 21
  • 4