IValueCOnverter is not working

For many hours he looked for this problem and did not see where I was wrong.

I have the following converter that just returns Brushes.Red (tried Colors.Red), 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 set the following namespace at the top:

xmlns:local="clr-namespace:Dashboard"

Now I have the following class bound 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));
        }
    }
}

Data binding (Value) works fine, but the converter does not want to kick, I tried to set a breakpoint in the Convert covnerter method, but this does not work when debugging, it just seems if my debugger is not called.

Can anyone shed some light on this?

+3
source share
2

, , , "Value" "color" .

+2

, . , , . , . , .

:

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.");
    }
}

UserControl.Resources:

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

StackPanel:

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

"", , - ? Bea Stollnitz . IValueConverters, -.

+1

Source: https://habr.com/ru/post/1732351/


All Articles