Jonathan Carroll
Jonathan Carroll

Reputation: 870

Telerik Data Column Foreground Color Converter Not Working

I'm having an issue changing the text color of a textbox in a column in a Telerik DataGrid.

XAML:

<telerik:RadGridView x:Name="RobotsGrid" AutoGenerateColumns="False" CanUserFreezeColumns="False" ItemsSource="{Binding Robots}" ShowGroupPanel="False" ShowInsertRow="False" RowIndicatorVisibility="Collapsed" CanUserDeleteRows="False" telerik:StyleManager.Theme="Office_Blue"  SelectionMode="Extended" CellEditEnded="RobotsGrid_CellEditEnded" EnableColumnVirtualization="False" Filtered="WorkItemsGrid_Filtered" SelectionChanged="RobotsGrid_SelectionChanged" EnableRowVirtualization="True" IsBusy="{Binding Path=RobotsRefreshing, Mode=OneWay}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Width="200" Header="Status" IsReadOnly="True">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Status}" Foreground="{Binding Path=Success, Converter={StaticResource BoolToColorConverter} }" IsReadOnly="True" IsTabStop="False" PreviewKeyDown="SelectKeyDown" PreviewMouseDown="SelectParentRow" TextWrapping="Wrap" Tag="{Binding}" BorderThickness="0" Margin="5,0"/>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>
             <telerik:RadGridView.Columns>
  </telerik:RadGridView>

The grid is bound to an ObservableCollection<Robots>, each Robot has a string Status and bool Success. I have bound the Text Property of the Texbox which works fine, but the Foreground is not being set properly. Robot implements INotifyProperty changed, and in the setter I am calling OnPropertyChanged().

I have hard-coded the Foreground and works as expected, it just seems as if the converter isn't working as expected.

Here's the converter:

public class BoolToColorConverter : IValueConverter {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        if (value != null) {
            var success = System.Convert.ToBoolean(value);
            if (success)
                return Brushes.Green;
            else
                return Brushes.Red;
        }

        return Brushes.Blue;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        return Brushes.Black;
    }
}

I'm not getting any errors, I've stepped through the code and the converter is hit, it returns Brushes.Green as expected, but I'm still not seeing the Foreground actually change. I have tried using the converter to return a string and use it on the Text property - it worked as expected.

Am I missing something completely obvious?

EDIT: After the converter is hit I am getting this in the output window :

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Success; DataItem='RobotManagerLite' (HashCode=21433709); target element is 'TextBox' (Name=''); target property is 'Foreground' (type 'Brush')

Upvotes: 1

Views: 697

Answers (1)

Jonathan Carroll
Jonathan Carroll

Reputation: 870

It was a silly problem - just a matter of using System.Windows.Media.Brushes instead of System.Drawing.Brushes. FML

Upvotes: 1

Related Questions