/// <summary> /// Checks whether an enum value matches one of the values in the <paramref name="range"/>. /// </summary> /// <param name="value">Value to test.</param> /// <param name="range">Range of valid values.</param> /// <returns>Returns true if the value is part of the range, otherwise false.</returns> public static bool In(this HtmlViewBindingMode value, IEnumerable <HtmlViewBindingMode> range) { if (range == null) { return(false); } return(range.Contains(value)); }
private Action <T> ViewSetterOrNullIfNotRequired <T>(Action <T> viewSetter, HtmlViewBindingMode bindingMode) { if (bindingMode.In(new[] { HtmlViewBindingMode.Command, HtmlViewBindingMode.OneWayToViewmodel })) { return(null); } else { return(viewSetter); } }
/// <summary> /// Creates a generic binding between an HTML element and a viewmodel. This method can /// achieve all behaviours the other bind methods can, but is more complex to use. /// All parameters are optional and null can be passed. /// </summary> /// <typeparam name="T">Type of the binding, use "object" if it is unimportant.</typeparam> /// <param name="viewSetter">Can write the property to the (HTML) view.</param> /// <param name="viewNotifier">Informs about changes/clicks in the (HTML) view.</param> /// <param name="viewmodelGetter">Can read the property from the viewmodel.</param> /// <param name="viewmodelSetter">Can write the property to the viewmodel.</param> /// <param name="viewmodelNotifier">Informs about changes in the viewmodel.</param> /// <param name="bindingMode">The binding mode which defines the direction of the binding.</param> public void BindGeneric <T>( Action <T> viewSetter, HtmlViewBindingViewNotifier viewNotifier, Func <T> viewmodelGetter, Action <T> viewmodelSetter, HtmlViewBindingViewmodelNotifier viewmodelNotifier, HtmlViewBindingMode bindingMode) { var binding = new HtmlViewBinding <T>( viewSetter, viewNotifier, viewmodelGetter, viewmodelSetter, viewmodelNotifier, bindingMode); binding.ValidateBindingModeOrThrow(); _bindings.Add(binding); }
/// <summary> /// Initializes a new instance of the <see cref="HtmlViewBinding{T}"/> class. /// </summary> /// <param name="viewSetter">Can write the property to the (HTML) view.</param> /// <param name="viewNotifier">Informs about changes/clicks in the (HTML) view.</param> /// <param name="viewmodelGetter">Can read the property from the viewmodel.</param> /// <param name="viewmodelSetter">Can write the property to the viewmodel.</param> /// <param name="viewmodelNotifier">Informs about changes in the viewmodel.</param> /// <param name="bindingMode">The binding mode which defines the direction of the binding.</param> public HtmlViewBinding( Action <T> viewSetter, HtmlViewBindingViewNotifier viewNotifier, Func <T> viewmodelGetter, Action <T> viewmodelSetter, HtmlViewBindingViewmodelNotifier viewmodelNotifier, HtmlViewBindingMode bindingMode) { _viewSetter = viewSetter; ViewNotifier = viewNotifier; _viewmodelGetter = viewmodelGetter; _viewmodelSetter = viewmodelSetter; BindingMode = bindingMode; ViewmodelNotifier = viewmodelNotifier; if (ViewNotifier != null) { ViewNotifier.Notified += ViewNotifiedEventHandler; } if (ViewmodelNotifier != null) { ViewmodelNotifier.Notified += ViewmodelNotifiedEventHandler; } }
private HtmlViewBindingViewNotifier CreateViewNotifierOrNullIfNotRequired(string bindingName, string jsEvent, HtmlViewBindingMode bindingMode) { if (bindingMode.In(new[] { HtmlViewBindingMode.Command, HtmlViewBindingMode.OneWayToView })) { return(null); } else { return(new HtmlViewBindingViewNotifier(bindingName, jsEvent)); } }
/// <summary> /// Binds a string property of the viewmodel to the background image of a control in the /// (HTML) view. This is a one way binding from the viewmodel to the view. The viewmodel /// property should contain the url to an image file, e.g. "background.png" /// </summary> /// <param name="bindingName">The name of the binding. The name is declared as /// "data-binding" attribute of the HTML element.</param> /// <param name="viewmodelGetter">Can read the property from the viewmodel.</param> /// <param name="viewmodelNotifier">Usually the viewmodel itself, supporting the /// INotifyPropertyChanged interface.</param> /// <param name="viewmodelPropertyName">Name of the property in the viewmodel, whose /// changes should be listened for.</param> /// <param name="bindingMode">The binding mode which defines the direction of the binding.</param> public void BindBackgroundImage(string bindingName, Func <string> viewmodelGetter, INotifyPropertyChanged viewmodelNotifier, string viewmodelPropertyName, HtmlViewBindingMode bindingMode) { if (!bindingMode.In(new[] { HtmlViewBindingMode.OneWayToView, HtmlViewBindingMode.OneWayToViewPlusOneTimeToView })) { throw new Exception("BindBackgroundImage expects the bindingMode to be either OneWayToView or OneWayToViewPlusOneTimeToView."); } BindGeneric <string>( (value) => ViewBackgroundImageSetter(bindingName, value), null, viewmodelGetter, null, CreateViewmodelNotifierOrNull(viewmodelNotifier, viewmodelPropertyName), bindingMode); }
/// <summary> /// Binds a boolean property of the viewmodel to the visibility state of a control in the /// (HTML) view. This is a one way binding from the viewmodel to the view. /// </summary> /// <param name="bindingName">The name of the binding. The name is declared as /// "data-binding" attribute of the HTML element.</param> /// <param name="viewmodelGetter">Can read the property from the viewmodel.</param> /// <param name="viewmodelNotifier">Usually the viewmodel itself, supporting the /// INotifyPropertyChanged interface.</param> /// <param name="viewmodelPropertyName">Name of the property in the viewmodel, whose /// changes should be listened for.</param> /// <param name="bindingMode">The binding mode which defines the direction of the binding.</param> public void BindVisibility(string bindingName, Func <bool> viewmodelGetter, INotifyPropertyChanged viewmodelNotifier, string viewmodelPropertyName, HtmlViewBindingMode bindingMode) { if (!bindingMode.In(new[] { HtmlViewBindingMode.OneWayToView, HtmlViewBindingMode.OneWayToViewPlusOneTimeToView })) { throw new Exception("BindVisibility expects the bindingMode to be either OneWayToView or OneWayToViewPlusOneTimeToView."); } BindGeneric <bool>( ViewSetterOrNullIfNotRequired <bool>((value) => ViewVisibilitySetter(bindingName, value), bindingMode), null, viewmodelGetter, null, CreateViewmodelNotifierOrNull(viewmodelNotifier, viewmodelPropertyName), bindingMode); }
/// <summary> /// Binds a boolean property of the viewmodel to a checkbox in the (HTML) view. /// The HTML element needs this properties: onclick="bind(event)" data-binding="MyBindingName". /// </summary> /// <param name="bindingName">The name of the binding. The name is declared as /// "data-binding" attribute of the HTML element.</param> /// <param name="viewmodelGetter">Can read the property from the viewmodel.</param> /// <param name="viewmodelSetter">Can write the property to the viewmodel.</param> /// <param name="viewmodelNotifier">Usually the viewmodel itself, supporting the /// INotifyPropertyChanged interface.</param> /// <param name="viewmodelPropertyName">Name of the property in the viewmodel, whose /// changes should be listened for.</param> /// <param name="bindingMode">The binding mode which defines the direction of the binding.</param> public void BindCheckbox(string bindingName, Func <bool> viewmodelGetter, Action <bool> viewmodelSetter, INotifyPropertyChanged viewmodelNotifier, string viewmodelPropertyName, HtmlViewBindingMode bindingMode) { if (!bindingMode.In(new[] { HtmlViewBindingMode.OneWayToViewmodel })) { throw new NotImplementedException("BindCheckbox expects the bindingMode to be OneWayToViewmodel, other modes are not supported."); } var binding = new CheckboxHtmlViewBinding( null, CreateViewNotifierOrNullIfNotRequired(bindingName, "click", bindingMode), viewmodelGetter, viewmodelSetter, CreateViewmodelNotifierOrNull(viewmodelNotifier, viewmodelPropertyName), bindingMode); _bindings.Add(binding); }
/// <summary> /// Binds a selected-element property of the viewmodel to a dropdown list in the (HTML) view. /// The HTML element needs this properties: onchange="bind(event)" data-binding="MyBindingName". /// The binding works with a string key, which can be converted by the <paramref name="viewmodelGetter"/> /// and the <paramref name="viewmodelSetter"/>. /// </summary> /// <param name="bindingName">The name of the binding. The name is declared as /// "data-binding" attribute of the HTML element.</param> /// <param name="viewmodelGetter">Can read the property from the viewmodel.</param> /// <param name="viewmodelSetter">Can write the property to the viewmodel.</param> /// <param name="viewmodelNotifier">Usually the viewmodel itself, supporting the /// INotifyPropertyChanged interface.</param> /// <param name="viewmodelPropertyName">Name of the property in the viewmodel, whose /// changes should be listened for.</param> /// <param name="bindingMode">The binding mode which defines the direction of the binding.</param> public void BindDropdown(string bindingName, Func <string> viewmodelGetter, Action <string> viewmodelSetter, INotifyPropertyChanged viewmodelNotifier, string viewmodelPropertyName, HtmlViewBindingMode bindingMode) { BindGeneric <string>( ViewSetterOrNullIfNotRequired <string>((value) => ViewValueSetter(bindingName, value), bindingMode), CreateViewNotifierOrNullIfNotRequired(bindingName, "change", bindingMode), viewmodelGetter, viewmodelSetter, CreateViewmodelNotifierOrNull(viewmodelNotifier, viewmodelPropertyName), bindingMode); }
/// <summary> /// Initializes a new instance of the <see cref="CheckboxHtmlViewBinding"/> class. /// </summary> /// <param name="viewSetter">Can write the property to the (HTML) view.</param> /// <param name="viewNotifier">Informs about changes/clicks in the (HTML) view.</param> /// <param name="viewmodelGetter">Can read the property from the viewmodel.</param> /// <param name="viewmodelSetter">Can write the property to the viewmodel.</param> /// <param name="viewmodelNotifier">Informs about changes in the viewmodel.</param> /// <param name="bindingMode">The binding mode which defines the direction of the binding.</param> public CheckboxHtmlViewBinding(Action <bool> viewSetter, HtmlViewBindingViewNotifier viewNotifier, Func <bool> viewmodelGetter, Action <bool> viewmodelSetter, HtmlViewBindingViewmodelNotifier viewmodelNotifier, HtmlViewBindingMode bindingMode) : base(viewSetter, viewNotifier, viewmodelGetter, viewmodelSetter, viewmodelNotifier, bindingMode) { }