在类别MVVM中:在添加属性Id1
仍然为null的新Customer
的情况下,当构造函数(不具有IdDocument
参数的构造函数(上存在匹配,导致ViewModel
没有Model
时,IdDocumentView
将具有IdDocumentViewModel
上下文。有没有一种标准的方法来处理这种情况,可能是将customer.Id1
设置为具有默认值的IdDocument
的实例,以便View
具有允许绑定以更新IdDocument
实例的值的上下文?
结构:
MainView : Catel.Windows.Controls.UserControl
_|--CustomerWindow : Catel.Windows.Window
___|--IdDocumentView : Catel.Windows.Controls.UserControl
型号类别:
public class Customer : Entity
{
[DomainSignature]
public string Code { get; set; }
public Gender Gender { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public DateTime DateOfBirth { get; set; }
public Lookup PlaceOfBirth { get; set; }
public string MobileNumber { get; set; }
public string Email { get; set; }
public ICollection<CustomerAddress> Addresses { get; set; }
public Lookup Occupation { get; set; }
public IdDocument Id1 { get; set; }
public IdDocument Id2 { get; set; }
}
public class IdDocument : Entity
{
[DomainSignature]
public Lookup IdType { get; set; }
[DomainSignature]
public string IdCode { get; set; }
[DomainSignature]
public DateTime IdExpiry { get; set; }
}
在CustomerView
中
<DockPanel HorizontalAlignment="Stretch" Grid.ColumnSpan="2">
<GroupBox Header="ID 1" DockPanel.Dock="Top" Margin="0">
<local:IdDocumentView DataContext="{Binding Id1}" HorizontalAlignment="Stretch" />
</GroupBox>
</DockPanel>
<orccontrols:EmptyColumn />
<DockPanel HorizontalAlignment="Stretch" Grid.ColumnSpan="2">
<GroupBox Header="ID 2" DockPanel.Dock="Top" Margin="0">
<local:IdDocumentView DataContext="{Binding Id2}" HorizontalAlignment="Stretch" />
</GroupBox>
</DockPanel>
IdDocumentView
<catel:UserControl x:Class="CTT.MTS.Views.IdDocumentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CTT.MTS.Views"
xmlns:catel="http://schemas.catelproject.com"
xmlns:orccontrols="http://schemas.wildgums.com/orc/controls"
xmlns:app="clr-namespace:CTT.MTS.Model"
xmlns:controls="clr-namespace:CTT.MTS.Controls"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="800">
<orccontrols:StackGrid>
<!-- Row definitions -->
<orccontrols:StackGrid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="40" />
<RowDefinition Height="40" />
</orccontrols:StackGrid.RowDefinitions>
<!-- Column definitions -->
<orccontrols:StackGrid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</orccontrols:StackGrid.ColumnDefinitions>
<Label Content="Type" />
<ComboBox ShouldPreserveUserEnteredPrefix="False" IsEditable="False" ItemsSource="{Binding IdTypes}"
DisplayMemberPath="Value" SelectedValuePath="Value" SelectedValue="{Binding IdTypeText}" Width="100"
HorizontalAlignment="Left">
</ComboBox>
<Label Content="Code" />
<TextBox Text="{Binding IdCode}" Width="100" HorizontalAlignment="Left"></TextBox>
<Label Content="Expiry" />
<orccontrols:DatePicker Width="120" HorizontalAlignment="Left" Margin="0" Height="40"
Value="{Binding IdExpiry, ValidatesOnDataErrors=True, NotifyOnValidationError=True}">
</orccontrols:DatePicker>
</orccontrols:StackGrid>
</catel:UserControl>
在MainViewModel
中用于Button
点击的命令方法
await uiVisualizerService.ShowAsync<CustomerViewModel>(new Customer
{
DateOfBirth = new DateTime(1981, 8, 8),
Gender = Gender.Male,
FirstName = "Muhammad",
Id1 = new IdDocument { IdType = idType, IdCode = "1111", IdExpiry = DateTime.Now.AddYears(-1) },
//Id2 = new IdDocument(),
Addresses = new List<CustomerAddress>(new[]
{new CustomerAddress {Address = address, AddressType = addressType, IsCurrent = true}})
});
注意,对Id2
的初始化进行了注释。如果像这样留下nullIdDocumentView
将没有到IdDocument
型号的链接
IdDocumentViewModel
的构造函数
public IdDocumentViewModel(ILookupService lookupService)
{
Argument.IsNotNull(() => lookupService);
this.lookupService = lookupService;
IdTypes = lookupService.IdTypes;
}
public IdDocumentViewModel(IdDocument idDocument, ILookupService lookupService) :this(lookupService)
{
IdDocument = idDocument;
IdTypeText = idDocument.IdType?.Value ?? "";
}
注意,我添加了第一个less arguments构造函数,因为我注意到当Customer.Id1
为null时,没有为其调用带有IdDocument
参数的构造函数。
有什么想法吗?
您的假设是正确的:
- Catel将首先尝试通过模型注入构建vm
- 如果这不可能,它将使用它能构造的参数最多的一个
- 如果不可能,则不会构建VM
在这种情况下,我通常会传入一个没有id的临时(新(模型。这样,"子vm"就知道它处于创建状态,而不是编辑状态。