1

所以我一直在尝试让保护条款与 Caliburn.Micro 和绑定的文本框一起使用。

风景:

<TextBox x:Name="UserAccount_DisplayName" Margin="-10,-5,-10,8"/>

<phone:PhoneApplicationPage.ApplicationBar>
  <shell:ApplicationBar IsVisible="True" IsMenuEnabled="False">
     <shell:ApplicationBar.Buttons>
        <cal:AppBarButton IconUri="\Resources\Iconography\appbar.check.rest.png"
                          Text="Save"
                          Message="SaveAndNavigateToAddAccountView" />
     </shell:ApplicationBar.Buttons>
  </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

视图模型:

public class EditAccountNameViewModel: PropertyChangedBase

    public Account UserAccount
    {
        get
        {
            return account;
        }
        set
        {
            account = value;
            NotifyOfPropertyChange(() => UserAccount);
            NotifyOfPropertyChange(() => CanSaveAndNavigateToAddAccountView);
        }
    }

    public bool CanSaveAndNavigateToAddAccountView
    {
        get
        {
            if (string.IsNullOrEmpty(UserAccount.DisplayName) == true)
            {
                return false;
            }

            return true;
        }   
    }

    public void SaveAndNavigateToAddAccountView()
    {
        CommitAccountToStorage();
        navigationService.UriFor<AddAccountViewModel>().Navigate();
    }

出于某种原因,在我开始输入文本框后,保护子句没有触发,这是我认为应该发生的。有任何想法吗?

4

1 回答 1

2

当您在文本框中键入内容然后选择另一个元素时(使文本框失去焦点),是否会触发保护子句?如果是这样,请尝试模拟绑定的 UpdateSourceTrigger=PropertyChanged 设置。请参阅Windows Phone 7 文本框的“UpdateSourceTrigger=PropertyChanged”等效项的答案,以了解如何模拟此行为。

编辑:我明白了,您正在(按照惯例)绑定到 UserAccount 的“DisplayName”属性。这意味着,当您在文本框中键入内容时,不会调用 EditAccountNameViewModel.UserAccount 属性的设置器。相反,将调用 UserAccount.DisplayName 上的设置器。我建议你做的是在你的 ViewModel 中创建另一个属性,比如 UserAccountDisplayName,它看起来像这样,然后绑定到它:

public string UserAccountDisplayName
{
   get { return UserAccount.DisplayName; }
   set 
   {
      UserAccount.DisplayName = value;
      NotifyOfPropertyChange(() => UserAccountDisplayName);
      NotifyOfPropertyChange(() => CanSaveAndNavigateToAddAccountView);
   }
}

这个 + 模拟 PropertyChanged 触发器应该可以工作。

于 2011-07-16T21:33:42.810 回答