locked
Converter for changing Textblock Foreground

    Question

  • Hello Everybody !!

    I need to change dynamically the foreground of a textblock.

    I developed a converter :

     public class BooleanToColorConverter : IValueConverter
      {
    
        public object Convert(object value, Type targetType, object parameter, string language)
        {
          if (value != null)
          {
            bool val = (bool)value;
            if (parameter != null && string.Compare(parameter as string, "calendar", StringComparison.CurrentCultureIgnoreCase) == 0)
            {
              return val == true ? new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)) : new SolidColorBrush(Color.FromArgb(0, 140,140,140));
            }
          
          }
          return false;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
          throw new NotImplementedException();
        }
      }

    And this is my XAML Code :

                    <TextBlock TextWrapping="Wrap" Grid.Column="1"
                               Foreground="{Binding IsWriteInRed, Converter={StaticResource BooleanToColorConverter}, ConverterParameter=calendar}" 
                               SelectionHighlightColor="{StaticResource WhiteSolidColorBrush}" Margin="5,0" VerticalAlignment="Center">
                      <Run Text="{Binding Company.Name}"/>
                      <Run Text=":"/>
                      <Run Text="{Binding Subject}"/>
                    </TextBlock>
    

    It doesn't work, I don't know why...any ideas please??

    Thanks

    Wednesday, March 25, 2015 10:49 AM

All replies

  • If the binding to the IsWriteInRed property fails, i.e. the property cannot be found on the DataContext of the TextBlock, the converter will never be invoked. You can specify a FallbackValue to be used if the binding fails:

            <TextBlock TextWrapping="Wrap" Grid.Column="1"
                               Foreground="{Binding IsWriteInRed, Converter={StaticResource BooleanToColorConverter}, ConverterParameter=calendar
                ,FallbackValue=Red}" 
                               SelectionHighlightColor="{StaticResource WhiteSolidColorBrush}" Margin="5,0" VerticalAlignment="Center">
                      <Run Text="{Binding Company.Name}"/>
                      <Run Text=":"/>
                      <Run Text="{Binding Subject}"/>
            </TextBlock>

    If the IsWriteInRed property is found it should work. It doesn't make much sense to ever return false from the Convert method though:

            public object Convert(object value, Type targetType, object parameter, string language)
            {
                bool val = (bool)value;
                if (parameter != null && string.Compare(parameter as string, "calendar", StringComparison.CurrentCultureIgnoreCase) == 0
                    && val)
                {
                    return new SolidColorBrush(Color.FromArgb(0, 255, 0, 0));
                }
    
                return new SolidColorBrush(Color.FromArgb(0, 140, 140, 140));
            }
    

     


    Please remember to close your threads by marking helpful posts as answer and then please start a new thread if you have a new question.

    Wednesday, March 25, 2015 1:27 PM
  • In what way is this not working? In addition to what Magnus said about the converter itself, is the IsWriteInRed property firing change notifications? If not then changes to it won't update the binding and trigger the converter.
    Wednesday, March 25, 2015 2:22 PM
    Moderator