Click here to Skip to main content
15,895,142 members
Articles / Desktop Programming / WPF

Piping Value Converters in WPF

Rate me:
Please Sign up or sign in to vote.
4.90/5 (48 votes)
14 Nov 2006CPOL8 min read 243.7K   2.6K   76  
Demonstrates how to chain together value converters used in WPF data binding.
<Window x:Class="PipedConverters.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:PipedConverters" 
    Title="PipedConverters" Height="300" Width="300"
    FontSize="18"
    >
  <Window.Resources>
    <!-- Loads the Tasks XML data. -->
    <XmlDataProvider x:Key="xmlData" Source="..\..\data.xml" XPath="Tasks/Task" />

    <!-- Converts the Status attribute text to the display name for that processing state. -->
    <local:ValueConverterGroup x:Key="statusDisplayNameGroup">
      <local:IntegerStringToProcessingStateConverter  />
      <local:EnumToDisplayNameConverter />
    </local:ValueConverterGroup>

    <!-- Converts the Status attribute text to a SolidColorBrush used to draw 
         the output of statusDisplayNameGroup. -->
    <local:ValueConverterGroup x:Key="statusForegroundGroup">
      <local:IntegerStringToProcessingStateConverter  />
      <local:ProcessingStateToColorConverter />
      <local:ColorToSolidColorBrushConverter />
    </local:ValueConverterGroup>

    <!-- Converts the Status attribute to the tooltip message for that processing state. -->
    <local:ValueConverterGroup x:Key="statusDescriptionGroup">
      <local:XmlAttributeToStringStateConverter />
      <local:IntegerStringToProcessingStateConverter  />
      <local:EnumToDescriptionConverter />
    </local:ValueConverterGroup>    

    <DataTemplate x:Key="taskItemTemplate">
      <StackPanel 
        Margin="2" 
        Orientation="Horizontal" 
        ToolTip="{Binding XPath=@Status, Converter={StaticResource statusDescriptionGroup}}"
        >
        <TextBlock Text="{Binding XPath=@Name}" />
        <TextBlock Text="   (" xml:space="preserve" />
        <TextBlock 
          Text="{Binding XPath=@Status, Converter={StaticResource statusDisplayNameGroup}}" 
          Foreground="{Binding XPath=@Status, Converter={StaticResource statusForegroundGroup}}" />
        <TextBlock Text=")" />
      </StackPanel>
    </DataTemplate>
  </Window.Resources>

  <Grid >
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition />
    </Grid.RowDefinitions>
    
    <TextBlock 
      Background="Black" 
      Foreground="White" 
      HorizontalAlignment="Stretch" 
      Text="Tasks" 
      TextAlignment="Center" />
    
    <ItemsControl
      Grid.Row="1"     
      DataContext="{StaticResource xmlData}"
      ItemsSource="{Binding}"
      ItemTemplate="{StaticResource taskItemTemplate}" />
  </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions