代码之家  ›  专栏  ›  技术社区  ›  AjLearning

为什么设置在ViewModel中聚焦,而不是在视图中聚焦条目

  •  1
  • AjLearning  · 技术社区  · 6 年前

    看起来ViewModel会更新条目的聚焦状态(基于用户交互),但在ViewModel中的绑定属性上设置值没有效果。

    我认为:

    <Entry IsFocused="{Binding Focused, Mode=TwoWay}"/>
    

    在我的ViewModel中:

    public bool Focused
    {
        get
        {
            return _focused;
        }
        set
        {
            _focused = value; // This gets invoked on user interaction with UI, but setting it programatically has no effect on the Entry's focus.
            OnPropertyChanged(nameof(Focused));
        }
    }
    


    为什么上面这些对我不起作用?

    是因为 IsFocussed 是只读的吗?如果是这样,为什么我甚至可以指定 Mode=TwoWay ?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Roubachof    6 年前
    public bool IsFocused
    {
        get { return (bool)GetValue(IsFocusedProperty); }
    }
    

    你猜对了 IsFocused 属性确实是只读的。

    它们确实可以在setter中抛出一个异常,但设计决策是困难的,必须在所有ui元素之间保持一致。

    推荐文章