IValueCOnverter Not Working
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 (PropertyChang开发者_如何学JAVAed != 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?
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.
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.
精彩评论