DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Validate Names Using Java
  • Maven Dependency Scope Applied
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • How to Convert a PDF to Text (TXT) Using Java

Trending

  • 5 Mistakes Companies Make When Working With Staff Engineers
  • PWA on iOS, Part 3: Chrome on iPhone Is WebKit Too. How One Engine Sets the Rules for PWAs on iOS
  • Full-Text Search Meets the Graph: Apache Lucene Inside Neo4j
  • When Production Stops Moving: Running Claude Code Across a Distributed Enterprise Integration Team

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. XAML and Converters Chaining

XAML and Converters Chaining

By 
Toni Petrina
·
Dec. 15, 14 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Converters are an essential building block in XAML interfaces with one simple task: converting values of one type to another. Since they have a input, usually a view model property, and an output, it would be wonderful if we could somehow chain them to create a new converter that processes all internal converters. Luckily, this is quite simple to do, but we do need to create a new converter which will hold other converters and whose implementation will iterate over nested converters. Full code can be found over at Github repository here, only interesting parts will be highlighted in this blog post.

Our combining converter class is also a converter itself, but it can contain other converters inside it:

[ContentProperty("Converters")]
public class ChainingConverter : IValueConverter
{
    public Collection<IValueConverter> Converters { get; set; }
}

Converter functions are trivially implemented and iteratively go through the converters list and apply the converter on the previous value.

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    foreach (var converter in Converters)
    {
        value = converter.Convert(value, targetType, parameter, culture);
    }
 
    return value;
}

ConvertBack is implemented in the same fashion.

This allows us to create new converters in XAML with the following syntax:

<c:ChainingConverter x:Key="InvertedBoolToVisibilityConverter">
    <c:InvertBoolConveter />
    <c:BooleanToVisibilityConverter />
</c:ChainingConverter>

But what if we need to send parameters to some of the converters, how can we do that when the same parameter is used throughout the ChainingConverter implementation? To provide custom parameter for individual converters, we can create a wrapper converter around existing converter and specify parameter on that wrapper. Here is a skeleton for such wrapper converter, notice that the wrapper is also a converter:

[ContentProperty("Converter")]
public class ParameterizedConverterWrapper : DependencyObject, IValueConverter
{
    // IValueConverter Converter dependency property
    // object Parameter dependency property
    // object DefaultReturnValue dependency property
     
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (Converter != null)
            return Converter.Convert(value, targetType, Parameter ?? parameter, culture);
        return DefaultReturnValue;
    }
}

Converter wrappers allow us to create complex converters such as this one:

<c:ChainingConverter x:Key="EnumToStringConverter">
     
    <c:Wrapper Parameter="First">
        <!-- return value == parameter; -->
        <c:EnumToBool />
    </c:Wrapper>
 
    <c:InvertBool />
     
    <c:BooleanToObject
        TrueValue="Hello"
        FalseValue="World"
        />
     
</c:ChainingConverter>

The final converter should be self explanatory even though you probably haven’t seen these converters before. You can see that unlike other converters, the wrapper is a dependency object which allows us to use bindings on the Parameter property since it is in fact a dependency property. More complex converters should be created from ordinary converters whenever possible, especially when working with primitive types such as bool, string, enums and null values.

What’s next?

The last example looked like a small DSL embedded in XAML. We could create converters that simulate flow control or conditionals. We could even create converters that switch depending on the property before it, essentially coding logic inside such converters. Whether that is desirable is debatable, but it can be done.

The full code with sample application can be found at the following Github repository: MassivePixel/wp-common.

 

Property (programming) View model Flow control (data) GitHub Repository (version control) Implementation Dependency application Strings

Published at DZone with permission of Toni Petrina. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Validate Names Using Java
  • Maven Dependency Scope Applied
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • How to Convert a PDF to Text (TXT) Using Java

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook