在Catel MVVM中添加新项目时的上下文



在类别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参数的构造函数。

有什么想法吗?

您的假设是正确的:

  1. Catel将首先尝试通过模型注入构建vm
  2. 如果这不可能,它将使用它能构造的参数最多的一个
  3. 如果不可能,则不会构建VM

在这种情况下,我通常会传入一个没有id的临时(新(模型。这样,"子vm"就知道它处于创建状态,而不是编辑状态。

相关内容

  • 没有找到相关文章

最新更新