嗨,美好的一天,我只是WPF和MVVM设计模式的新手,我从PRISM的BRIAN LAGUNAS先生的博客和视频中学到了很多东西..但只是想问一个noob问题..我的代码有什么问题吗doest为我工作...非常感谢任何帮助谢谢.这是我的代码:
我的观点模型
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; }
}
Run Code Online (Sandbox Code Playgroud)
基于myModel
声明为另一个类文件
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
查看 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>
Run Code Online (Sandbox Code Playgroud)
我已经读过这个但它对我不起作用..并且无法理解我怎样才能正确编码...请帮我理解这件事..希望对于任何回复很快..thx
小智 4
1)你不能像你想要的那样使用复杂的数据模型,所以尝试一下
private myPErson _MyPerson;
public myPErson MyPerson
{
get { return _MyPerson; }
set
{
if (_MyPerson != null)
_MyPerson.PropertyChanged -= MyPersonOnPropertyChanged;
SetProperty(ref _MyPerson, value);
if (_MyPerson != null)
_MyPerson.PropertyChanged += MyPersonOnPropertyChanged;
}
}
private void MyPersonOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
updateCommand.RaiseCanExecuteChanged();
}
Run Code Online (Sandbox Code Playgroud)
2)改变你的构造函数
public Person()
{
MyPerson = new myPErson();
updateCommand = new DelegateCommand(Execute, CanExecute);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6687 次 |
| 最近记录: |