我使用prism 6在WPF中开发应用程序。我尝试使用此命令更新日期时间。我尝试更新代码而没有任何成功。
编辑1: 添加了前链接到Onedrive的代码
类:
public class Employee : BindableBase
{
private int _id;
public int ID
{
get { return _id; }
set
{
SetProperty(ref _id, value);
}
}
private string _firstName ="YehudaEmp";
public string FirstName
{
get { return _firstName; }
set {SetProperty(ref _firstName , value); }
}
private string _lastName="DnaiEmp";
public string LastName
{
get { return _lastName; }
set
{
SetProperty(ref _lastName, value);
}
}
private int _phone;
public int Phone
{
get
{
return _phone;
}
set
{
SetProperty(ref _phone, value);
}
}
private string _email;
public string Email
{
get { return _email; }
set
{
SetProperty(ref _email, value);
}
}
private string _city;
public string City
{
get { return _city; }
set
{
SetProperty(ref _city, value);
}
}
private string _street;
public string Street
{
get { return _street; }
set
{
SetProperty(ref _street, value);
}
}
private string _state;
public string State
{
get { return _state; }
set
{
SetProperty(ref _state, value);
}
}
private DateTime? _lastUpdate;
public DateTime? LastUpdate
{
get { return _lastUpdate; }
set
{
SetProperty(ref _lastUpdate, value);
}
}
}
public class EmployeeViewModel :BindableBase
{
private Employee emop;
public string FirstName
{
get { return emop.FirstName; }
set
{
emop.FirstName = value;
}
}
public string LastName
{
get { return emop.LastName; }
set
{
emop.LastName = value;
}
}
public DateTime? lastUpdated
{
get { return emop.LastUpdate; }
set
{
emop.LastUpdate = value;
}
}
public DelegateCommand updateCommand { get; set; }
public EmployeeViewModel()
{
emop = new Employee();
updateCommand = new DelegateCommand(Execute , canExecute).ObservesProperty(()=>FirstName).ObservesProperty(() =>LastName);
}
private bool canExecute()
{
return !String.IsNullOrWhiteSpace(FirstName) && !String.IsNullOrWhiteSpace(LastName);
}
private void Execute()
{
lastUpdated = DateTime.Now;
}
}
XAML:
<Grid>
<StackPanel>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Grid.Column="0" Grid.Row="0" Width="195" Height="30" Margin="5" Text="{Binding FirstName ,UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Grid.Column="0" Grid.Row="1" Width="195" Height="30" Margin="5" Text="{Binding LastName , UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text="{Binding lastUpdated,UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" Grid.Column="1" FontSize="20"/>
<Button Name="updatebtn" Grid.Row="3" Grid.Column="1" Width="Auto" Height="30" Margin="5" Content="update" Command="{Binding updateCommand,Mode=TwoWay}"/>
</Grid>
</StackPanel>
</Grid>
任何帮助?
答案 0 :(得分:1)
假设您的视图模型是绑定到视图的EmployeeViewModel
。
然后,您的问题是您的视图模型没有将PropertyChanged
事件上升到视图中。您正在从模型中升级属性更改事件Employee
并且视图未捕获这些事件。
您需要从视图模型中为视图引发事件以捕获它们。
方法BindableBase.SetProperty<T>
引发PropertyChanged
事件,因此您只需将SetProperty<T>
类的Employee
调用移至EmployeeViewModel
类
因此,例如,您的lastUpdated
绑定:
<TextBlock Text="{Binding lastUpdated,UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" Grid.Column="1" FontSize="20"/>
视图模型中应该具有如下所示的相应属性:
private DateTime? m_lastUpdated;
public DateTime? lastUpdated
{
get { return m_lastUpdated; }
set { SetProperty<DateTime?>(ref m_lastUpdated, value);
}
从MVVM的角度来看,您通常不会从模型中引发PropertyChanged
个事件(在您的情况下为Employee
),唯一应该与视图通信的实体是视图模型(在你的情况下EmployeeViewModel
。