保护条款未触发
所以我一直在尝试让保护子句与 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>
ViewModel:
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();
}
由于某种原因,在我开始在文本框中键入内容后,保护子句没有触发,这是我认为应该发生的情况。有什么想法吗?
So I have been trying to get guard clauses to work with Caliburn.Micro and a bound textbox.
The View:
<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>
The ViewModel:
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();
}
For some reason the guard clause is not firing after I begin typing in the textbox, which is what I would have assumed should happen. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在文本框中键入内容然后选择另一个元素(以便文本框失去焦点)时,保护子句是否会触发?如果是这样,请尝试模拟绑定的 UpdateSourceTrigger=PropertyChanged 设置。请参阅 Windows Phone 7 的 "UpdateSourceTrigger=PropertyChanged" 等效项的答案TextBox 查看如何模拟此行为。
编辑:我看到,您正在绑定(按照惯例)到 UserAccount 的“DisplayName”属性。这意味着,当您在文本框中键入内容时,不会调用 EditAccountNameViewModel.UserAccount 属性的设置器。相反,将调用 UserAccount.DisplayName 上的 setter。我建议您做的是在 ViewModel 中创建另一个属性,例如 UserAccountDisplayName,它看起来像这样,然后绑定到它:
This + 模拟 PropertyChanged 触发器应该可以工作。
Does the guard clause fire when you type something in the textbox and then select another element (so that textbox loses focus)? If so, try simulating UpdateSourceTrigger=PropertyChanged setting of a binding. See anwsers to "UpdateSourceTrigger=PropertyChanged" equivalent for a Windows Phone 7 TextBox to see how to simulate this behavior.
EDIT: I see, that you are binding (by convention) to a "DisplayName" property of UserAccount. This means, that setter of EditAccountNameViewModel.UserAccount property will not be called when you type something in the textbox. Instead, a setter on UserAccount.DisplayName will be called. What I'd suggest you to do is to create another property in your ViewModel, say UserAccountDisplayName, that would look sth like this, and bind to it instead:
This + simulating PropertyChanged trigger should work.