所以我一直在尝试让保护条款与 Caliburn.Micro 和绑定(bind)的文本框一起使用。
风景:
<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>
View 模型:
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();
}
出于某种原因,在我开始输入文本框后,保护子句没有触发,这是我认为应该发生的。有任何想法吗?
最佳答案
当您在文本框中键入内容然后选择另一个元素时(使文本框失去焦点),是否会触发保护子句?如果是这样,请尝试模拟绑定(bind)的 UpdateSourceTrigger=PropertyChanged 设置。请参阅 "UpdateSourceTrigger=PropertyChanged" equivalent for a Windows Phone 7 TextBox 的回复看看如何模拟这种行为。
编辑:我明白了,您正在(按照惯例)绑定(bind)到 UserAccount 的“DisplayName”属性。这意味着,当您在文本框中键入内容时,不会调用 EditAccountNameViewModel.UserAccount 属性的 setter 。相反,将调用 UserAccount.DisplayName 上的 setter 。我建议你做的是在你的 ViewModel 中创建另一个属性,比如 UserAccountDisplayName,它看起来像这样,然后绑定(bind)到它:
public string UserAccountDisplayName
{
get { return UserAccount.DisplayName; }
set
{
UserAccount.DisplayName = value;
NotifyOfPropertyChange(() => UserAccountDisplayName);
NotifyOfPropertyChange(() => CanSaveAndNavigateToAddAccountView);
}
}
这个 + 模拟 PropertyChanged 触发器应该可以工作。
关于c# - 保护条款不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6720196/