none
Multibinding to change TextBlock Foreground color

    Question

  • Hi

    Can anyone kindly help a WPF newbie with this problem?

    I'm trying to change the foreground color based on the values of an objects properties.
    The Converter is being called and logic is correct, but the foreground color does not change.

    XAML:

    <
    local
    :
    FontColorConverter
     x:Key
    =
    "
    fontColorConverter
    "
     />
    <
    br
    />
    
    <
    HierarchicalDataTemplate
     x:Key
    =
    "
    itemTemplate
    "
     ItemsSource
    =
    "
    {Binding ClientPublications}
    "
    >
    
          <
    StackPanel
     Orientation
    =
    "
    Horizontal
    "
    >
    
              <
    TextBlock
     Text
    =
    "
    {Binding Name}
    "
    >
                        
                  <
    TextBlock.Foreground
    >
    
                      <
    MultiBinding
     Converter
    =
    "
    {StaticResource fontColorConverter}
    "
     >
    
                          <
    Binding
     Path
    =
    "
    HasBeenRead
    "
     Mode
    =
    "
    TwoWay
    "
     />
    
                          <
    Binding
     Path
    =
    "
    MustRead
    "
     Mode
    =
    "
    TwoWay
    "
     />
    
                      </
    MultiBinding
    >
                                                         
                  </
    TextBlock.Foreground
    >
    
              </
    TextBlock
    >
    
              <
    TextBlock
     Margin
    =
    "
    5,0,0,0
    "
     
                         FontWeight
    =
    "
    Bold
    "
     Visibility
    =
    "
    {Binding Path=HasBeenRead, Converter={StaticResource unreadVisibilityConverter}, Mode=TwoWay}
    "
    
                         Text
    =
    "
    {Binding Path=ClientPublications, Converter={StaticResource unreadCountConverter}, Mode=TwoWay}
    "
    >
    
              </
    TextBlock
    >
    
          </
    StackPanel
    >
    
    </
    HierarchicalDataTemplate
    >
    
    

    CS:

    public
     class
     FontColorConverter : IMultiValueConverter
        {
            #region
     IMultiValueConverter Members
    
            public
     object
     Convert(object
    [] value, Type targetType, object
     parameter, System.Globalization.CultureInfo culture)
            {
                bool
    ? hasBeenRead = value[0] as
     Nullable<bool
    >;
                bool
    ? mustRead = value[1] as
     Nullable<bool
    >;
    
                if
     ((mustRead.HasValue && hasBeenRead.HasValue) && (hasBeenRead.Value == false
    ))
                {
    
                    return
     mustRead.Value ? "Red"
     : "Black"
    ;                
                }
                else
    
                {
                    // Fallback value
    
                    return
     "Black"
    ;
                }
            }
    
            public
     object
    [] ConvertBack(object
     value, Type[] targetType, object
     parameter, System.Globalization.CultureInfo culture)
            {
                throw
     new
     NotImplementedException();
            }
    
            #endregion
    
        }
    







    • Edited by Ryan Emptage Tuesday, August 11, 2009 10:28 AM spacing
    Tuesday, August 11, 2009 10:28 AM

Answers

  • Hi,

    Instead of returning a string with the color name in your converter, you should return a Brush because the Foreground property expects a brush object. In your XAML your write the color name but the XAML parser then converts this into a Brush behind the scenes.

    So in your converter you should return for example:

     

    return Brushes.Red;

    Hope it helps.

    Regards,


    Federico Benitez
    • Marked as answer by Ryan Emptage Tuesday, August 11, 2009 10:58 AM
    Tuesday, August 11, 2009 10:43 AM

All replies

  • Hi,

    Instead of returning a string with the color name in your converter, you should return a Brush because the Foreground property expects a brush object. In your XAML your write the color name but the XAML parser then converts this into a Brush behind the scenes.

    So in your converter you should return for example:

     

    return Brushes.Red;

    Hope it helps.

    Regards,


    Federico Benitez
    • Marked as answer by Ryan Emptage Tuesday, August 11, 2009 10:58 AM
    Tuesday, August 11, 2009 10:43 AM
  • aaaaaaaah yes, thank you.
    Tuesday, August 11, 2009 10:58 AM