-1

I'm trying to binding the TextColor from a label on ViewCell:

Label myLabel = new Label { Text = "SomeText" };

myLabel.SetBinding(Label.TextColorProperty,
    new Binding("TheTextColor", BindingMode.TwoWay, new LabelTextColorConverter()));

Here's the converter:

public class LabelTextColorConverter : IValueConverter
{
    public bool OldValue { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        OldValue = (bool) value;
        Debug.WriteLine("asdadasdsadsada");
        if ((bool)value)
            return Color.Red;
        else
            return Color.Silver;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Debug.WriteLine("qwqweqewqeeqe");
        return OldValue;
    }
}

The debug output doesn't appear, and the color doesn't change either. I don't see anything wrong.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Ineltec Android
  • 79
  • 1
  • 2
  • 5
  • 2
    Without [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve), it would not be possible to say what's wrong. I will say that it seems from your code example that you are binding to a `Label` instance that's not actually in your UI (i.e. you create it on the fly and don't attach it anywhere), and you are binding to the wrong property path (should be "TextColor", not "TheTextColor"). – Peter Duniho Jul 03 '15 at 23:11
  • This is a short version of the code. I don`t send here, I have another UI`s on code and the others properties from viewModel works fine, like TextProperty. The text from Label is black, so the conditional on converter and the converter doesn`t work. Above this binding I have: `myLabel.SetBinding(Label.TextProperty, "TheNames");` It works fine... – Ineltec Android Jul 06 '15 at 12:29
  • Please read the link I provided to understand what is mean by "good, _minimal_, _complete_ code example", as well as for information on why such a code example is required. Please read https://stackoverflow.com/help/how-to-ask for more information on how to present your question in a clear, answerable way. – Peter Duniho Jul 06 '15 at 15:16

1 Answers1

0

Why do you need Two Way binding for this? I don't think it's necessary.

myLabel.SetBinding(Label.TextColorProperty, new Binding("TheTextColor", BindingMode.OneWay, new LabelTextColorConverter()));

Then:

public class LabelTextColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool val = (bool)value;

        if (val)
            return Color.Red;
        else
            return Color.Silver;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

... and it should work fine. Also make sure you're setting BindingContext for your page/controls correctly.

Daniel Luberda
  • 7,374
  • 1
  • 32
  • 40