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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Comparing SaaS vs. PaaS for Kafka and Flink Data Streaming
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  • Overcoming React Development Hurdles: A Guide for Developers
  • Why We Still Struggle With Manual Test Execution in 2025
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. XAML and Converters Chaining

XAML and Converters Chaining

By 
Toni Petrina user avatar
Toni Petrina
·
Dec. 15, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
4.8K Views

Join the DZone community and get the full member experience.

Join For Free

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, DZone MVB. 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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!