【发布时间】:2015-11-10 06:33:29
【问题描述】:
嗨,美好的一天,我是 WPF 和 MVVM 设计模式的新手,我从 PRISM 中 BRIAN LAGUNAS 爵士的博客和视频中学到了很多东西。但只是想问一个菜鸟问题。有什么问题我的代码对我不起作用......非常感谢任何帮助。 这是我的代码:
我的视图模型
public class Person : BindableBase
{
private myPErson _MyPerson;
public myPErson MyPerson
{
get { return _MyPerson; }
set
{
SetProperty(ref _MyPerson, value);
}
}
public Person()
{
_MyPerson = new myPErson();
updateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyPerson.FirstName).ObservesProperty(() => MyPerson.Lastname);
// updateCommand = new DelegateCommand(Execute).ObservesCanExecute((p) => CanExecute); /// JUST WANNA TRY THIS BUT DUNNO HOW
}
private bool CanExecute()
{
return !String.IsNullOrWhiteSpace(MyPerson.FirstName) && !String.IsNullOrWhiteSpace(MyPerson.Lastname);
}
private void Execute()
{
MessageBox.Show("HOLA");
}
public DelegateCommand updateCommand { get; set; }
}
我的模型
声明到另一个类文件
public class myPErson : BindableBase
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
SetProperty(ref _firstName, value);
}
}
private string _lastname;
public string Lastname
{
get { return _lastname; }
set
{
SetProperty(ref _lastname, value);
}
}
}
查看 Xaml 代码
<Window x:Class="Prism6Test.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myVM="clr-namespace:Prism6Test.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<myVM:Person x:Key="mainVM"/>
</Window.Resources>
<Grid DataContext="{StaticResource mainVM}">
<TextBox HorizontalAlignment="Left" Height="23" Margin="217,103,0,0" TextWrapping="Wrap" Text="{Binding MyPerson.FirstName,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="217,131,0,0" TextWrapping="Wrap" Text="{Binding MyPerson.Lastname,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<Button Content="Button" Command="{Binding updateCommand}" HorizontalAlignment="Left" Margin="242,159,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
我已经读过这个,但它对我不起作用..并且无法理解我如何正确编码它..请帮助我解决这个问题..希望尽快回复..thx
ObservesProperty method isn't observing model's properties at Prism 6
【问题讨论】:
-
按钮在做什么?
-
发布消息框“HOLA”。
-
好的。现在我看到了。代码有什么问题?您没有在 TextBoxes 中看到任何内容?
-
它被绑定到一个 DelegateCommand 属性。它应该执行“执行”方法..但在它执行之前,按钮被禁用,直到两个文本框被填满,谢谢你评论@JensHorstmann
-
它被绑定到一个 DelegateCommand 属性。它应该执行“执行”方法..但在它执行之前,按钮被禁用,直到两个文本框被填满......但在我的情况下它不起作用..谢谢你的评论@JensHorstmann