WPF

WPF IValueConverter: inverse Visibility and enum display names

S. Iguchi WPF Notes s-iguchi
WPF

Two custom IValueConverters show how to invert bool-to-Visibility bindings and read enum display names from Description attributes in WPF. The article measures which types ConverterParameter actually receives, how Hidden and Collapsed differ in layout, and which exceptions an implementation without null checks throws.

Key points

  • ConverterParameter=True written in XAML reaches Convert as a System.String, so a bool comparison never inverts.
  • The built-in BooleanToVisibilityConverter exposes no settable properties; inverting or Hidden needs a custom converter.
  • TrueValue and FalseValue properties let XAML pick Visible, Collapsed or Hidden per converter instance.
  • Hidden keeps the 40 px Border's space (55.96 px); Collapsed pulls the next TextBlock up to 15.96 px.
  • A bool parameter arrives typed only via {StaticResource True} (sys:Boolean) or {x:Static Visibility.Hidden}.
  • Enum values without a Description attribute, or undefined values, throw when the converter lacks null checks.

Why it matters

This is one of WPF's oldest recurring mistakes, and the article earns its place by measuring behaviour instead of just describing it. Desktop apps in maintenance mode tend to have converters written once and never revisited, so a silent wrong result can survive for years across views and screens.

Choosing between a display property on the view model and a converter is really a question about localisation and testing: if nobody asserts on XAML, keeping readable text in the view model gives a unit test something to reach. Before adopting, audit your own visibility bindings and cover null sources and attribute-less enum members.

Real-world use

  • An order screen that hides or disables a busy indicator while processing runs, driven by a bool on the view model.
  • A status ComboBox bound to enum values that must display readable text instead of member names.
  • Forms where hiding a field must not move the inputs below it, so Hidden is preferred over Collapsed.

This summary was generated by the agent from the original post on S. Iguchi WPF Notes. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message