/// <summary> /// Saves the state of the control. /// </summary> public void SaveControlState(JToken viewModelToken, DotvvmControl control) { if (control is DotvvmBindableControl && ((DotvvmBindableControl)control).RequiresControlState) { // find the control state collection var controlStateCollection = viewModelToken["$controlState"]; if (controlStateCollection == null) { controlStateCollection = new JObject(); ((JObject)viewModelToken).Add("$controlState", controlStateCollection); } // find the control state item control.EnsureControlHasId(); var controlState = controlStateCollection[control.ID] as JObject; if (controlState == null) { controlState = new JObject(); ((JObject)controlStateCollection).Add(control.ID, controlState); } // serialize the property values foreach (var item in ((DotvvmBindableControl)control).ControlState.Where(v => v.Value != null)) { controlState.Add(item.Key, JToken.FromObject(item.Value)); controlState.Add(item.Key + "$type", JToken.FromObject(item.Value.GetType())); } } }
public static CommandBindingExpression RegisterExtensionCommand(this DotvvmControl control, Delegate action, string methodUsageId) { control.EnsureControlHasId(); var id = control.ID + methodUsageId; var propertyName = control.GetType().FullName + "/" + methodUsageId; var property = DotvvmProperty.Register <object, ExtensionCommands>(propertyName); var binding = new CommandBindingExpression(action, id); control.SetBinding(property, binding); return(binding); }
/// <summary> /// Loads the state of the control. /// </summary> public void LoadControlState(JToken viewModelToken, DotvvmControl control) { if (control is DotvvmBindableControl && ((DotvvmBindableControl)control).RequiresControlState) { // find the control state var controlStateCollection = viewModelToken["$controlState"]; if (controlStateCollection != null) { control.EnsureControlHasId(); var controlState = controlStateCollection[control.ID] as JObject; if (controlState != null) { // fill the collection with property values foreach (var item in controlState.Properties().Where(p => !p.Name.EndsWith("$type"))) { var type = Type.GetType(controlState[item.Name + "$type"].Value <string>()); ((DotvvmBindableControl)control).ControlState[item.Name] = item.Value.ToObject(type); } } } } }