3
votes

I created a custom renderer for a Label. Now I wanted to manipulate with its focus state, so I created bindable property and event. It works fine when I change the bindable property from custom renderer like so: Element.IsFocused = true; But when I change it from the view model, it affects XAML view, but for some reasons doesn't call the setter for this property. Here is the code:

In custom class:

 public new static readonly BindableProperty IsFocusedProperty =
           BindableProperty.Create(nameof(IsFocused), typeof(bool), typeof(FloatingEntry), false);

        public new bool IsFocused
        {
            get { return (bool)GetValue(IsFocusedProperty); }
            set
            {
                SetValue(IsFocusedProperty, value);
                if (value) Focus();
            }
        }  

In XAML:

IsFocused="{Binding PasswordEntryIsFocused}"

In View Model:

 private bool _passwordEntryIsFocused;
         public bool PasswordEntryIsFocused
            {
                get { return _passwordEntryIsFocused; }
                set
                {
                    SetProperty(ref _passwordEntryIsFocused, value);
                }
            }

In View Model in some method: PasswordEntryIsFocused = true;

It's not about new keyword, I tried without it. And binding works, because I tried to bind it with a visual property, like IsVisible and it was working like it should, but setter is always called only from a custom renderer.

I think I may miss something in a context of bindable property work.

1

1 Answers

8
votes

But when I change it from the view model, it affects XAML view, but for some reasons doesn't call the setter for this property.

Yes, that is a common mistake with WPF. The XAML generated code does not call the setter, instead it changes the bound dependency property immediately. You can't break on this event, unless you attach the PropertyChangedCallback event.