views:

440

answers:

2

Hi There,

Been googling this problem for hours, and cannot see where I am going wrong.

I have the following converter which just returns Brushes.Red(have tried Colors.Red) as well but still no luck.

public class ColorConverter : IValueConverter
{
    private static ColorConverter instance = new ColorConverter();
    public static ColorConverter Instance
    {
        get
        {
            return instance;
        }
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Brushes.Red;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}

Now in my xaml I have the following code:

<StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding Value}" TextAlignment="Center" Foreground="{Binding Path=color, Converter={x:Static local:ColorConverter.Instance}}" Margin="2"/>
</StackPanel>

I have set teh following namespace at the top:

xmlns:local="clr-namespace:Dashboard"

Now I have the following class that is binded to the stack panel:

public class MyClass : INotifyPropertyChanged
{
    public String Value;
    public Color color;

    // Declare the PropertyChanged event
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

The data binding (Value) works perfectly fine, but the converter does not want to kick in, I tried to set a breakpoint in the Convert method of the covnerter, but that does not get triggered when debugging, it just seems as if my debugger isn't being called.

Can anyone shed some light on this?

+1  A: 

Well, here's how I've done it in my project. I modified my code to reflect what you're trying to do. I hope it helps. I can't answer why your singleton approach wouldn't work.

Class:

public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Brushes.Red;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}

In my UserControl.Resources element:

<UserControl.Resources>
    <local:ColorConverter x:Key="MyColorConverter" />
</UserControl.Resources>

StackPanel element:

<StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding Value}" TextAlignment="Center" Foreground="{Binding Path=color, Converter={StaticResource MyColorConverter}}" Margin="2"/>
</StackPanel>

Also, did you check your Output window to see if there are any errors? You should also read Bea Stollnitz's article on debugging databindings. She actually has a specific section on IValueConverters that might come in handy for you some day.

Dave
Thanks for that! Found the problem after going into my output window.I didnt know that to make a variable a property you have to add { set; get; } to it. So I didnt have them there and output complained that it cannot find "color" property.Really appreciate the help, and thanks for the link. Added to favourites!
LnDCobra
ah yes, you did use a field instead of a property. I have made that mistake before (and just did again, apparently). :)
Dave
+1  A: 

I'm surprised that you say that the binding itself works because "Value" and "color" are fields and binding to fields is not supposed to work.

Daniel Pratt
yeah, I didn't even bother to look at their declarations because it sounded like everything but the converter was working. :)
Dave