我使用此模型类创建了一个动态表单:
public class CustomField
{
[JsonProperty("uid")]
public Guid uid { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("type")]
public CustomFieldType type { get; set; }
}
public enum CustomFieldType
{
TEXT = 0,
MULTI_LINE = 1,
DATETIME = 2,
...
}
,我将使用此模型类保存自定义字段值:
public class CustomFieldValue
{
[JsonProperty("uid")]
public Guid uid { get; set; }
[JsonProperty("type")]
public CustomFieldType type { get; set; }
[JsonProperty("textValue")]
public string textValue { get; set; }
[JsonProperty("dateValue")]
public DateTime dateValue { get; set; }
...
}
除自定义字段外,我的基本模型还包含标准输入:
public class BaseModel
{
[JsonProperty("uid")]
public Guid uid { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("description")]
public string description { get; set; }
...
[JsonProperty("customFieldValues")]
public List<CustomFieldValue> customFieldValues { get; set; }
}
我的问题是,如何像下面在视图模型中那样使用标准输入以编程方式绑定自定义字段:
public class ViewModel : ViewModelBase
{
BaseModel cModel;
...
public string name
{
get
{
if (isLoaded)
return Model.name;
else
return "";
}
set
{
if (Model.name != value)
{
Model.name = value;
OnPropertyChanged();
isModified = true;
}
}
}
...
public BaseModel Model
{
get
{
return cModel;
}
set
{
cModel = value;
OnPropertyChanged();
OnPropertyChanged(nameof(name));
...
}
}
}
在我的查看页面:
<TextBox x:Name="txtName"
Text="{x:Bind ViewModel.name, Mode=TwoWay}"
Style="{StaticResource entryDefault}"
Header= "Name*"
PlaceholderText="Enter Name"
Margin="0,10,0,0"
Width="800"
HorizontalAlignment="Left"/>