/// <summary> /// Binds a nullable enum scalar data to <see cref="CheckBox"/>. /// </summary> /// <typeparam name="T">The enum type.</typeparam> /// <param name="source">The source scalar data.</param> /// <param name="enumMemberValue">The value of enum member.</param> /// <param name="title">The title of the CheckBox. If null, the name of enum member will be used.</param> /// <returns>The scalar binding object.</returns> public static ScalarBinding <CheckBox> BindToCheckBox <T>(this Scalar <T?> source, T enumMemberValue, object title = null) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new ArgumentException(DiagnosticMessages.BindingFactory_EnumTypeRequired(nameof(T)), nameof(T)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } return(new ScalarBinding <CheckBox>(onRefresh: v => v.IsChecked = source.GetValue().HasFlag(enumMemberValue), onSetup: v => { v.Content = title ?? Enum.GetName(typeof(T), enumMemberValue); }, onCleanup: null) .BeginInput(new PropertyChangedTrigger <CheckBox>(CheckBox.IsCheckedProperty), new ExplicitTrigger <CheckBox>()) .WithFlush(source, v => { var value = source.GetValue(); var newValue = value.GetNewValue(enumMemberValue, v, true); if (Comparer <T?> .Default.Compare(value, newValue) == 0) { return false; } source.EditValue(newValue); return true; }) .EndInput()); }
/// <summary> /// Binds a scalar data to <see cref="ComboBox"/>. /// </summary> /// <typeparam name="T">The data type of the column.</typeparam> /// <param name="source">The source scalar data.</param> /// <param name="dropDownList">Data of drop-down list.</param> /// <param name="selectedValuePath">The path that is used to get the selected value from drop-down list.</param> /// <param name="displayMemberPath">The path to serve as the visual representation of the drop-down list item.</param> /// <returns>The scalar binding object.</returns> public static ScalarBinding <ComboBox> BindToComboBox <T>(this Scalar <T> source, Scalar <IEnumerable> dropDownList, string selectedValuePath = "Value", string displayMemberPath = "Display") { source.VerifyNotNull(nameof(source)); dropDownList.VerifyNotNull(nameof(dropDownList)); if (string.IsNullOrEmpty(selectedValuePath)) { throw new ArgumentNullException(nameof(selectedValuePath)); } if (string.IsNullOrEmpty(displayMemberPath)) { throw new ArgumentNullException(nameof(displayMemberPath)); } return(new ScalarBinding <ComboBox>( onSetup: (v, p) => { v.SelectedValuePath = selectedValuePath; v.DisplayMemberPath = displayMemberPath; }, onRefresh: (v, p) => { v.ItemsSource = dropDownList.Value; v.SelectedValue = source.GetValue(); }, onCleanup: (v, p) => { v.ItemsSource = null; v.SelectedValuePath = null; v.DisplayMemberPath = null; }).WithInput(ComboBox.SelectedValueProperty, source, e => (T)e.SelectedValue)); }
/// <summary> /// Binds nullable <see cref="Double"/> scalar data to <see cref="TextBox"/>. /// </summary> /// <param name="source">The source nullabled <see cref="Double"/> scalar data.</param> /// <param name="flushErrorMessage">The conversion error message when flushing data from binding to source model.</param> /// <returns>The scalar binding object.</returns> public static ScalarBinding <TextBox> BindToTextBox(this Scalar <Double?> source, string flushErrorMessage = null) { return(new ScalarBinding <TextBox>(onSetup: v => v.Setup(), onCleanup: v => v.Cleanup(), onRefresh: v => { if (!v.GetIsEditing()) { v.Text = source.GetValue().ToString(); } }).BeginInput(TextBox.TextProperty, TextBox.LostFocusEvent) .WithFlushingValidator(v => { if (string.IsNullOrEmpty(v.Text)) { return null; } Double result; return Double.TryParse(v.Text, out result) ? null : GetInvalidInputErrorMessage(flushErrorMessage, typeof(Double)); }) .WithFlush(source, v => { if (string.IsNullOrEmpty(v.Text)) { return null; } else { return Double.Parse(v.Text); } }) .EndInput()); }
/// <summary> /// Binds string scalar data to <see cref="TextBox"/>. /// </summary> /// <param name="source">The source string scalar data.</param> /// <returns>The scalar binding object.</returns> public static ScalarBinding <TextBox> BindToTextBox(this Scalar <String> source) { return(new ScalarBinding <TextBox>(onSetup: v => v.Setup(), onCleanup: v => v.Cleanup(), onRefresh: v => { if (!v.GetIsEditing()) { v.Text = source.GetValue(); } }).WithInput(TextBox.TextProperty, TextBox.LostFocusEvent, source, v => v.Text)); }
/// <summary> /// Binds string scalar data to <see cref="PasswordBox"/>. /// </summary> /// <param name="source">The source scalar data.</param> /// <returns>The scalar binding object.</returns> public static ScalarBinding <PasswordBox> BindToPasswordBox(this Scalar <string> source) { return(new ScalarBinding <PasswordBox>(onRefresh: (v, p) => { var password = source.GetValue(); if (v.Password != password) // PasswordBox.Password is not a dependency property, update only when value changed. { v.Password = password; } }).WithInput(PasswordBox.PasswordChangedEvent, PasswordBox.LostFocusEvent, source, v => v.Password)); }
/// <summary> /// Binds scalar data to <see cref="TextBlock"/>. /// </summary> /// <param name="source">The source scalar data.</param> /// <param name="format">A composite format string.</param> /// <param name="formatProvider">An object that supplies culture-specific formatting information.</param> /// <returns>The scalar binding object.</returns> public static ScalarBinding <TextBlock> BindToTextBlock(this Scalar source, string format = null, IFormatProvider formatProvider = null) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(new ScalarBinding <TextBlock>( onRefresh: v => { v.Text = source.GetValue().ToString(format, formatProvider); })); }
/// <summary> /// Binds a scalar data to <see cref="CheckBox"/>. /// </summary> /// <param name="source">The source scalar data.</param> /// <param name="title">The title of the CheckBox.</param> /// <returns>The scalar binding object.</returns> public static ScalarBinding <CheckBox> BindToCheckBox(this Scalar <bool> source, object title = null) { if (source == null) { throw new ArgumentNullException(nameof(source)); } return(new ScalarBinding <CheckBox>(onRefresh: v => v.IsChecked = source.GetValue(), onSetup: v => { if (title != null) { v.Content = title; } }, onCleanup: null) .BeginInput(new PropertyChangedTrigger <CheckBox>(CheckBox.IsCheckedProperty)) .WithFlush(source, v => v.IsChecked.Value) .EndInput()); }
/// <summary> /// Binds a nullable DateTime scalar data to <see cref="DatePicker"/>. /// </summary> /// <param name="source">The source scalar data.</param> /// <returns>The scalar binding object.</returns> public static ScalarBinding <DatePicker> BindToDatePicker(this Scalar <DateTime?> source) { return(new ScalarBinding <DatePicker>(onRefresh: (v, p) => v.SelectedDate = source.GetValue()) .WithInput(DatePicker.SelectedDateProperty, source, v => v.SelectedDate)); }