private static FrameworkElement GenerateMaskedTextBox(MaskedElement maskedElement) { MaskedTextBox maskedTextBox = new MaskedTextBox(); maskedTextBox.DataContext = maskedElement; maskedTextBox.SetBinding(MaskedTextBox.MaskProperty, new Binding("Mask")); maskedTextBox.SetBinding(MaskedTextBox.TextProperty, new Binding("Value") { Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }); maskedTextBox.SetBinding(MaskedTextBox.WatermarkTextProperty, new Binding("Prompt")); maskedTextBox.Height = 25; maskedTextBox.MinWidth = 100; return(maskedTextBox); }
public override void OnApplyTemplate() { base.OnApplyTemplate(); _datePickerTextbox = new DatePickerTextBox(); _masktextbox = new MaskedTextBox(); _datePickerTextbox = this.Template.FindName("PART_TextBox", this) as DatePickerTextBox; _datePickerTextbox.Visibility = System.Windows.Visibility.Hidden; Grid _grid = new Grid(); _grid = (Grid)_datePickerTextbox.Parent; _grid.Children.Insert(1, _masktextbox); #region MaskTextBox Properties Configuration _masktextbox.Mask = "00/00/0000"; _masktextbox.AutoSelectBehavior = AutoSelectBehavior.OnFocus; _masktextbox.AutoMoveFocus = true; _masktextbox.InsertKeyMode = InsertKeyMode.Overwrite; #endregion #region CodeBehind TwoWays DataBinding Configuration Binding datebinding = new Binding("date"); datebinding.Source = this; datebinding.Mode = BindingMode.TwoWay; datebinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; #endregion #region Set DataBinding to DatePicker And MaskTextBox _datePickerTextbox.SetBinding(DatePickerTextBox.TextProperty, datebinding); _masktextbox.SetBinding(MaskedTextBox.TextProperty, datebinding); #endregion #region Subscribe Events for MaskTextBox if (_masktextbox != null) { _masktextbox.TextChanged += _datePickerTextbox_TextChanged; _masktextbox.PreviewKeyDown += _masktextbox_PreviewKeyDown; } #endregion }
public static TextBox GenerateExtraTextBox(Element element) { var textBox = new MaskedTextBox() { Width = 250, DataContext = element, Margin = new Thickness(10, 0, 0, 0) }; textBox.SetBinding(MaskedTextBox.TextProperty, new Binding("ExtraText") { Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }); textBox.WatermarkText = "Enter any extra details..."; textBox.Height = 25; return(textBox); }
/// <summary> /// Creates the extended toolkit control. /// </summary> /// <param name="propertyDefinition">The property definition.</param> /// <param name="bindingPath">The binding path.</param> /// <returns>A FrameworkElement.</returns> public FrameworkElement CreateExtendedToolkitControl(PropertyDefinition propertyDefinition, string bindingPath) { var propertyType = propertyDefinition.PropertyType; if (propertyType.Is(typeof(DateTime))) { var c = new DateTimePicker { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, IsReadOnly = propertyDefinition.IsReadOnly }; c.SetBinding(DateTimePicker.ValueProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } if (propertyType.Is(typeof(TimeSpan))) { var c = new TimeSpanUpDown { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, IsReadOnly = propertyDefinition.IsReadOnly }; c.SetBinding(TimeSpanUpDown.ValueProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } if (propertyType.Is(typeof(int)) || propertyType.Is(typeof(int?))) { var c = new CalculatorUpDown { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Minimum = int.MinValue, Maximum = int.MaxValue, IsReadOnly = propertyDefinition.IsReadOnly }; c.SetBinding(CalculatorUpDown.ValueProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } if (propertyType.Is(typeof(uint)) || propertyType.Is(typeof(uint?))) { var c = new CalculatorUpDown { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Minimum = 0, Maximum = uint.MaxValue, IsReadOnly = propertyDefinition.IsReadOnly }; c.SetBinding(CalculatorUpDown.ValueProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } if (propertyType.Is(typeof(decimal)) || propertyType.Is(typeof(decimal?))) { var c = new CalculatorUpDown { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Minimum = decimal.MinValue, Maximum = decimal.MaxValue, IsReadOnly = propertyDefinition.IsReadOnly }; c.SetBinding(CalculatorUpDown.ValueProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } if (propertyType.Is(typeof(float)) || propertyType.Is(typeof(float?))) { var c = new CalculatorUpDown { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Minimum = decimal.MinValue, Maximum = decimal.MaxValue, IsReadOnly = propertyDefinition.IsReadOnly }; c.SetBinding(CalculatorUpDown.ValueProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } if (propertyType.Is(typeof(double)) || propertyType.Is(typeof(double?))) { var c = new CalculatorUpDown { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Minimum = decimal.MinValue, Maximum = decimal.MaxValue, IsReadOnly = propertyDefinition.IsReadOnly }; c.SetBinding(CalculatorUpDown.ValueProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } if (propertyType.Is(typeof(Brush))) { var c = new ColorBox.ColorBox { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, }; c.SetBinding(ColorBox.ColorBox.BrushProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } if (propertyType.Is(typeof(Guid)) || propertyType.Is(typeof(Guid?))) { var c = new MaskedTextBox { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Mask = "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA", IsReadOnly = propertyDefinition.IsReadOnly }; c.SetBinding(TextBox.TextProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } if (propertyType.Is(typeof(char)) || propertyType.Is(typeof(char?))) { var c = new MaskedTextBox { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Stretch, Mask = "&", IsReadOnly = propertyDefinition.IsReadOnly }; c.SetBinding(TextBox.TextProperty, propertyDefinition.CreateBinding(bindingPath)); return(c); } return(null); }
// TODO: Infer Null value type to handle public static FrameworkElement GetBsonValueEditor( OpenEditorMode openMode, string bindingPath, BsonValue bindingValue, object bindingSource, bool readOnly, string keyName) { var binding = new Binding { Path = new PropertyPath(bindingPath), Source = bindingSource, Mode = BindingMode.TwoWay, Converter = new BsonValueToNetValueConverter(), UpdateSourceTrigger = UpdateSourceTrigger.Explicit }; if (bindingValue.IsArray) { var arrayValue = bindingValue as BsonArray; if (openMode == OpenEditorMode.Window) { var button = new Button { Content = $"[Array] {arrayValue?.Count} {keyName}", Style = StyleKit.MaterialDesignEntryButtonStyle }; button.Click += (s, a) => { arrayValue = bindingValue as BsonArray; var windowController = new WindowController { Title = "Array Editor" }; var control = new ArrayEntryControl(arrayValue, readOnly, windowController); var window = new DialogWindow(control, windowController) { Owner = Application.Current.MainWindow, Height = Math.Min(Math.Max(636, SystemParameters.VirtualScreenHeight / 1.61), SystemParameters.VirtualScreenHeight), MinWidth = 400, MinHeight = 400, }; if (window.ShowDialog() == true) { arrayValue?.Clear(); arrayValue?.AddRange(control.EditedItems); button.Content = $"[Array] {arrayValue?.Count} {keyName}"; } }; return(button); } var contentView = new ContentExpander { LoadButton = { Content = $"[Array] {arrayValue?.Count} {keyName}" } }; contentView.LoadButton.Click += (s, a) => { if (contentView.ContentLoaded) { return; } arrayValue = bindingValue as BsonArray; var control = new ArrayEntryControl(arrayValue, readOnly); control.CloseRequested += (sender, args) => { contentView.Content = null; }; contentView.Content = control; }; return(contentView); } if (bindingValue.IsDocument) { var expandLabel = "[Document]"; if (openMode == OpenEditorMode.Window) { var button = new Button { Content = expandLabel, Style = StyleKit.MaterialDesignEntryButtonStyle }; button.Click += (s, a) => { var windowController = new WindowController { Title = "Document Editor" }; var bsonDocument = bindingValue as BsonDocument; var control = new DocumentEntryControl(bsonDocument, readOnly, windowController); var window = new DialogWindow(control, windowController) { Owner = Application.Current.MainWindow, Height = Math.Min(Math.Max(636, SystemParameters.VirtualScreenHeight / 1.61), SystemParameters.VirtualScreenHeight), MinWidth = 400, MinHeight = 400, }; window.ShowDialog(); }; return(button); } var contentView = new ContentExpander { LoadButton = { Content = expandLabel } }; contentView.LoadButton.Click += (s, a) => { if (contentView.ContentLoaded) { return; } var bsonDocument = bindingValue as BsonDocument; var control = new DocumentEntryControl(bsonDocument, readOnly); control.CloseRequested += (sender, args) => { contentView.Content = null; }; contentView.Content = control; }; return(contentView); } if (bindingValue.IsBoolean) { var check = new CheckBox { IsEnabled = !readOnly, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; check.SetBinding(ToggleButton.IsCheckedProperty, binding); return(check); } if (bindingValue.IsDateTime) { var datePicker = new DateTimePicker { TextAlignment = TextAlignment.Left, IsReadOnly = readOnly, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0), }; datePicker.SetBinding(DateTimePicker.ValueProperty, binding); return(datePicker); } if (bindingValue.IsDouble) { var numberEditor = new DoubleUpDown { TextAlignment = TextAlignment.Left, IsReadOnly = readOnly, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; numberEditor.SetBinding(DoubleUpDown.ValueProperty, binding); return(numberEditor); } if (bindingValue.IsDecimal) { var numberEditor = new DecimalUpDown { TextAlignment = TextAlignment.Left, IsReadOnly = readOnly, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; numberEditor.SetBinding(DecimalUpDown.ValueProperty, binding); return(numberEditor); } if (bindingValue.IsInt32) { var numberEditor = new IntegerUpDown { TextAlignment = TextAlignment.Left, IsReadOnly = readOnly, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; numberEditor.SetBinding(IntegerUpDown.ValueProperty, binding); return(numberEditor); } if (bindingValue.IsInt64) { var numberEditor = new LongUpDown { TextAlignment = TextAlignment.Left, IsReadOnly = readOnly, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; numberEditor.SetBinding(LongUpDown.ValueProperty, binding); return(numberEditor); } if (bindingValue.IsGuid || bindingValue.Type == BsonType.Guid) { var guidEditor = new MaskedTextBox { IsReadOnly = readOnly, VerticalAlignment = VerticalAlignment.Center, Mask = @"AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA", ValueDataType = typeof(Guid) }; guidEditor.SetBinding(MaskedTextBox.ValueProperty, binding); return(guidEditor); } if (bindingValue.IsString) { var stringEditor = new TextBox { IsReadOnly = readOnly, AcceptsReturn = true, VerticalAlignment = VerticalAlignment.Center, MaxHeight = 200, MaxLength = 1024, VerticalScrollBarVisibility = ScrollBarVisibility.Auto, }; stringEditor.SetBinding(TextBox.TextProperty, binding); return(stringEditor); } if (bindingValue.IsBinary) { var text = new TextBlock { Text = "[Binary Data]", VerticalAlignment = VerticalAlignment.Center, }; return(text); } if (bindingValue.IsObjectId) { var text = new TextBox { Text = bindingValue.AsString, IsReadOnly = true, VerticalAlignment = VerticalAlignment.Center, }; return(text); } var defaultEditor = new TextBox { VerticalAlignment = VerticalAlignment.Center, MaxHeight = 200, MaxLength = 1024, VerticalScrollBarVisibility = ScrollBarVisibility.Auto, }; defaultEditor.SetBinding(TextBox.TextProperty, binding); return(defaultEditor); }