可绑定属性的设置器永远不会触发 [英] Setter of bindable property never fires

查看:38
本文介绍了可绑定属性的设置器永远不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Label创建了一个自定义渲染器.现在,我想使用其焦点状态进行操作,因此创建了可绑定的属性和事件.当我像这样从自定义渲染器更改可绑定属性时,它可以正常工作: Element.IsFocused = true; 但是,当我从视图模型更改它时,它会影响XAML视图,但是由于某些原因,它不会调用此属性的setter方法.这是代码:

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:

在自定义课程中:

 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();
            }
        }  

在XAML中:

IsFocused="{Binding PasswordEntryIsFocused}"

在视图模型中:

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

在View模型中通过某些方法: PasswordEntryIsFocused = true;

In View Model in some method: PasswordEntryIsFocused = true;

它与 new 关键字无关,我尝试了不使用它的情况.绑定有效,因为我试图将其与可视属性(如 IsVisible )进行绑定,并且按应有的方式工作,但是setter始终仅从自定义渲染器调用.

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.

推荐答案

但是当我从视图模型更改它时,它会影响XAML视图,但是由于某些原因,它不会调用此属性的设置方法.

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.

是的,这是WPF的常见错误.XAML生成的代码不会调用setter,而是会立即更改绑定的依赖项属性.除非附加了 PropertyChangedCallback 事件,否则您无法中断此事件.

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.

这篇关于可绑定属性的设置器永远不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆