在Catel.MVVM中:
如果添加新的Customer
且属性Id1
仍然为空,则IdDocumentView
的上下文为IdDocumentViewModel
匹配构造函数(不具有IdDocument
参数的构造函数),导致没有ViewModel
的{{1}}。是否有解决此情况的标准方法,可能是通过将Model
设置为具有默认值的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}})
});
的初始化已注释。如果像这样留下 null Id2
,则不会链接到IdDocumentView
模型
IdDocument
的构造函数
IdDocumentViewModel
请注意,我注意到第一个 less自变量构造函数是在通知该构造函数为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 ?? "";
}
且该参数为null时未为其调用的IdDocument
参数之后添加的。
有什么想法吗?
答案 0 :(得分:0)
您的假设是正确的:
在这种情况下,我通常会传入没有ID的临时(新)模型。这样,“子虚拟机”就知道它处于创建状态,而不是编辑状态。