private void FormatNumericElement(object sender, RoutedEventArgs e, string format) { MahApps.Metro.Controls.NumericUpDown t = (MahApps.Metro.Controls.NumericUpDown)sender; double d = 0; //if(Double.TryParse(t.Text,out d)) //{ // if (format.Contains("P")) // d = d / 100; // t.Text = d.ToString(format); // t.BorderThickness = new Thickness(1); // t.BorderBrush = Brushes.LightGray; //} //else //{ // t.BorderThickness = new Thickness(2); // t.BorderBrush = Brushes.Red; //} }
private void FormViewer_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (!(this.DataContext is FormInstance)) { return; } ///Ok so the datacontex wil be a Forminstance FormInstance f = (FormInstance)this.DataContext; //does this forminstance already have dataitems //We clear all the existing elements from the Grid FormGrid.Children.Clear(); //Set up the rows and columns FormGrid.RowDefinitions.Clear(); FormGrid.ColumnDefinitions.Clear(); var rows = f.FormTemplate.Rows.Split(','); for (int i = 0; i < f.FormTemplate.NumberOfRows; i++) { RowDefinition def = new RowDefinition(); Binding widthBinding = new Binding("ActualHeight"); widthBinding.Source = FormGrid; widthBinding.Converter = new FractionConverter(); widthBinding.ConverterParameter = Convert.ToDouble(rows[i]); def.SetBinding(RowDefinition.HeightProperty, widthBinding); FormGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(Convert.ToDouble(rows[i]), GridUnitType.Star) }); } var cols = f.FormTemplate.Columns.Split(','); for (int i = 0; i < f.FormTemplate.NumberOfColumns; i++) { ColumnDefinition def = new ColumnDefinition(); Binding widthBinding = new Binding("ActualWidth"); widthBinding.Source = FormGrid; widthBinding.Converter = new FractionConverter(); widthBinding.ConverterParameter = Convert.ToDouble(cols[i]); def.SetBinding(ColumnDefinition.WidthProperty, widthBinding); //FormGrid.ColumnDefinitions.Add(def); FormGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(Convert.ToDouble(cols[i]), GridUnitType.Star) }); } ///Now we iterate the elements that make up this template ///and add them to the grid ///for each form element, we add a control, and we check if the FormInstance already has a ///QADataItem that matches in terms of its DataDictionaryEntry and EquipmentItem ///if we have match, then we assign that dataitem to the Control we just added foreach (FormElement elm in f.FormTemplate.FormElements) { if (elm is LabelFormElement) { LabelFormElement l = (LabelFormElement)elm; Label X = new Label(); X.HorizontalContentAlignment = HorizontalAlignment.Center; X.VerticalContentAlignment = VerticalAlignment.Center; X.Content = l.LabelText; setControlLayout(l, X); } else if (elm is BooleanFormElement) { BooleanFormElement b = (BooleanFormElement)elm; CheckBox X = new CheckBox(); QADataItem qaDataItem = null; if (f.DataItems.Where(x => x.DataDictionaryEntry.FullName == b.DataDictionaryEntry.FullName).Any()) { //qaDataItem = (QADataItem)f.DataItems.Where(x => x.DataDictionaryEntry.FullName == b.DataDictionaryEntry.FullName).First(); } else { //qaDataItem = new QADataItem(); //qaDataItem.EquipmentItem = b.EquipmentItem; } setControlLayout(b, X); } else if (elm is NumericFormElement) { ///create refernce to native type NumericFormElement n = (NumericFormElement)elm; ///Create the control MahApps.Metro.Controls.NumericUpDown t = new MahApps.Metro.Controls.NumericUpDown(); t.Maximum = n.Maximum; t.MinHeight = n.Minimum; t.StringFormat = "{}{0:F" + n.NumberOfDecimals.ToString() + "} " + n.Unit; ///We should never have a NumericFormElement without a DataDictionaryEntry if (n.DataDictionaryEntry != null) { ///This object is the underlying QADataItem that the control will bind to. QADataItem d = null; if (f.DataItems.Where(x => x.DataDictionaryEntry.FullName == n.DataDictionaryEntry.FullName && (x as QADataItem).EquipmentItem.FullName == n.EquipmentItem.FullName).Any()) { ///This FormInstance already has a QADataItem that matches on DataDictionaryEntry and EquipmentItem, so let's retrieve that one. d = f.DataItems.Where(x => x.DataDictionaryEntry.FullName == n.DataDictionaryEntry.FullName && (x as QADataItem).EquipmentItem.FullName == n.EquipmentItem.FullName).First() as QADataItem; } else { d = new QuantifiableQADataItem { EquipmentItem = n.EquipmentItem, DataDictionaryEntry = n.DataDictionaryEntry, QAFormInstance = f as QAFormInstance }; } Binding bd = new Binding("Value"); bd.Source = d; bd.Mode = BindingMode.TwoWay; BindingOperations.SetBinding(t, MahApps.Metro.Controls.NumericUpDown.ValueProperty, bd); } else { // / we should log this } //t.LostFocus += new RoutedEventHandler((senderX, eX) => FormatNumericElement(senderX, eX, n.Format + n.NumberOfDecimals)); setControlLayout(n, t); } else if (elm is TextFormElement) { TextFormElement tfe = (TextFormElement)elm; TextBox t = new TextBox(); setControlLayout(tfe, t); } } }
public IEnumerable <FrameworkElement> GetControls() { PropertyInfo[] props = GetType().GetProperties(); foreach (PropertyInfo prop in props) { if (!prop.CanWrite || !prop.CanRead) { continue; } string propName = prop.Name; Type propType = prop.PropertyType; string localizedName; string key1 = $"{Type}_{propName}"; string key2 = $"Shared_{propName}"; if (LocalizationManager.Current.Reward.ContainsKey(key1)) { localizedName = LocalizationManager.Current.Reward.Translate(key1); } else if (LocalizationManager.Current.Reward.ContainsKey(key2)) { localizedName = LocalizationManager.Current.Reward.Translate(key2); } else { localizedName = key1; } Grid borderContents = new Grid(); Label l = new Label { Content = localizedName }; TooltipAttribute rewardTooltip = prop.GetCustomAttribute <TooltipAttribute>(); if (rewardTooltip != null) { l.ToolTip = rewardTooltip.Text; } else if (LocalizationManager.Current.Reward.ContainsKey($"{key1}_Tooltip")) { l.ToolTip = LocalizationManager.Current.Reward[$"{key1}_Tooltip"]; } else if (LocalizationManager.Current.Reward.ContainsKey($"{key2}_Tooltip")) { l.ToolTip = LocalizationManager.Current.Reward[$"{key2}_Tooltip"]; } RangeAttribute rangeAttribute = prop.GetCustomAttribute <RangeAttribute>(); AssetPickerAttribute assetPickerAttribute = prop.GetCustomAttribute <AssetPickerAttribute>(); borderContents.Children.Add(l); FrameworkElement valueControl = null; if (propType == typeof(uint)) { valueControl = new MahApps.Metro.Controls.NumericUpDown() { Maximum = uint.MaxValue, Minimum = uint.MinValue, ParsingNumberStyle = System.Globalization.NumberStyles.Integer, HideUpDownButtons = true }; (valueControl as MahApps.Metro.Controls.NumericUpDown).SetBinding(MahApps.Metro.Controls.NumericUpDown.ValueProperty, propName); } else if (propType == typeof(byte?)) { byte newMax, newMin; if (rangeAttribute != null && rangeAttribute.Maximum is byte rMax && rangeAttribute.Minimum is byte rMin) { newMax = rMax; newMin = rMin; } else { newMax = byte.MaxValue; newMin = byte.MinValue; } valueControl = new Controls.OptionalByteValueControl(); (valueControl as Controls.OptionalByteValueControl).upDown.Maximum = newMax; (valueControl as Controls.OptionalByteValueControl).upDown.Minimum = newMin; (valueControl as Controls.OptionalByteValueControl).upDown.SetBinding(Xceed.Wpf.Toolkit.ByteUpDown.ValueProperty, propName); }
public IEnumerable <FrameworkElement> GetControls() { PropertyInfo[] props = GetType().GetProperties(); foreach (PropertyInfo prop in props) { if (!prop.CanWrite || !prop.CanRead) { continue; } string propName = prop.Name; Type propType = prop.PropertyType; string localizedName = LocalizationManager.Current.Condition[$"{Type}_{propName}"]; Grid borderContents = new Grid(); Label l = new Label { Content = localizedName }; ConditionTooltipAttribute conditionTooltip = prop.GetCustomAttribute <ConditionTooltipAttribute>(); if (conditionTooltip != null) { l.ToolTip = conditionTooltip.Text; } borderContents.Children.Add(l); FrameworkElement valueControl = null; if (propType == typeof(ushort)) { valueControl = new MahApps.Metro.Controls.NumericUpDown() { Maximum = ushort.MaxValue, Minimum = ushort.MinValue, ParsingNumberStyle = System.Globalization.NumberStyles.Integer, HideUpDownButtons = true }; (valueControl as MahApps.Metro.Controls.NumericUpDown).SetBinding(MahApps.Metro.Controls.NumericUpDown.ValueProperty, propName); } else if (propType == typeof(uint)) { valueControl = new MahApps.Metro.Controls.NumericUpDown() { Maximum = uint.MaxValue, Minimum = uint.MinValue, ParsingNumberStyle = System.Globalization.NumberStyles.Integer, HideUpDownButtons = true }; (valueControl as MahApps.Metro.Controls.NumericUpDown).SetBinding(MahApps.Metro.Controls.NumericUpDown.ValueProperty, propName); } else if (propType == typeof(int)) { valueControl = new MahApps.Metro.Controls.NumericUpDown() { Maximum = int.MaxValue, Minimum = int.MinValue, ParsingNumberStyle = System.Globalization.NumberStyles.Integer, HideUpDownButtons = true }; (valueControl as MahApps.Metro.Controls.NumericUpDown).SetBinding(MahApps.Metro.Controls.NumericUpDown.ValueProperty, propName); } else if (propType == typeof(short)) { valueControl = new MahApps.Metro.Controls.NumericUpDown() { Maximum = short.MaxValue, Minimum = short.MinValue, ParsingNumberStyle = System.Globalization.NumberStyles.Integer, HideUpDownButtons = true }; (valueControl as MahApps.Metro.Controls.NumericUpDown).SetBinding(MahApps.Metro.Controls.NumericUpDown.ValueProperty, propName); } else if (propType == typeof(ushort)) { valueControl = new MahApps.Metro.Controls.NumericUpDown() { Maximum = ushort.MaxValue, Minimum = ushort.MinValue, ParsingNumberStyle = System.Globalization.NumberStyles.Integer, HideUpDownButtons = true }; (valueControl as MahApps.Metro.Controls.NumericUpDown).SetBinding(MahApps.Metro.Controls.NumericUpDown.ValueProperty, propName); } else if (propType == typeof(float)) { valueControl = new MahApps.Metro.Controls.NumericUpDown() { Maximum = float.MaxValue, Minimum = float.MinValue, ParsingNumberStyle = System.Globalization.NumberStyles.Float, HideUpDownButtons = true }; (valueControl as MahApps.Metro.Controls.NumericUpDown).SetBinding(MahApps.Metro.Controls.NumericUpDown.ValueProperty, propName); } else if (propType == typeof(byte)) { valueControl = new MahApps.Metro.Controls.NumericUpDown() { Maximum = byte.MaxValue, Minimum = byte.MinValue, ParsingNumberStyle = System.Globalization.NumberStyles.Integer, HideUpDownButtons = true }; (valueControl as MahApps.Metro.Controls.NumericUpDown).SetBinding(MahApps.Metro.Controls.NumericUpDown.ValueProperty, propName); } else if (propType == typeof(string)) { valueControl = new TextBox() { MaxWidth = 100, TextWrapping = TextWrapping.Wrap }; (valueControl as TextBox).SetBinding(TextBox.TextProperty, propName); } else if (propType.IsEnum) { ComboBox cBox = new ComboBox(); Array values = Enum.GetValues(propType); foreach (object eValue in values) { cBox.Items.Add(LocalizationManager.Current.Condition[$"{Type}_{prop.Name}_{eValue.ToString()}"]); } cBox.SetBinding(ComboBox.SelectedItemProperty, new Binding() { Converter = new EnumItemsSource() { Dictionary = LocalizationManager.Current.Condition, LocalizationPrefix = $"{Type}_{prop.Name}_", Type = propType }, Path = new PropertyPath(propName), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }); valueControl = cBox; } else if (propType == typeof(bool)) { valueControl = new CheckBox() { }; (valueControl as CheckBox).SetBinding(CheckBox.IsCheckedProperty, propName); } else { App.Logger.Log($"{propName} does not have required type '{propType.FullName}'"); } valueControl.HorizontalAlignment = HorizontalAlignment.Right; valueControl.VerticalAlignment = VerticalAlignment.Center; borderContents.Children.Add(valueControl); valueControl.Tag = "variable::" + propName; Border b = new Border { Child = borderContents }; b.Margin = new Thickness(0, 5, 0, 5); if (prop.GetCustomAttribute <ConditionOptionalAttribute>() != null) { b.Opacity = 0.75; } yield return(b); } }
public IEnumerable <FrameworkElement> GetControls() { PropertyInfo[] props = GetType().GetProperties(); foreach (PropertyInfo prop in props) { if (!prop.CanWrite || !prop.CanRead) { continue; } string propName = prop.Name; Type propType = prop.PropertyType; // string localizedName = LocalizationManager.Current.Condition[$"{Type}_{propName}"]; string localizedName; string key1 = $"{Type}_{propName}"; string key2 = $"Shared_{propName}"; if (LocalizationManager.Current.Condition.ContainsKey(key1)) { localizedName = LocalizationManager.Current.Condition.Translate(key1); } else if (LocalizationManager.Current.Condition.ContainsKey(key2)) { localizedName = LocalizationManager.Current.Condition.Translate(key2); } else { localizedName = key1; } Grid borderContents = new Grid(); Label l = new Label { Content = localizedName }; TooltipAttribute conditionTooltip = prop.GetCustomAttribute <TooltipAttribute>(); if (conditionTooltip != null) { l.ToolTip = conditionTooltip.Text; } else if (LocalizationManager.Current.Condition.ContainsKey($"{key1}_Tooltip")) { l.ToolTip = LocalizationManager.Current.Condition[$"{key1}_Tooltip"]; } else if (LocalizationManager.Current.Condition.ContainsKey($"{key2}_Tooltip")) { l.ToolTip = LocalizationManager.Current.Condition[$"{key2}_Tooltip"]; } borderContents.Children.Add(l); RangeAttribute rangeAttribute = prop.GetCustomAttribute <RangeAttribute>(); AssetPickerAttribute assetPickerAttribute = prop.GetCustomAttribute <AssetPickerAttribute>(); FrameworkElement valueControl = null; if (propType == typeof(ushort)) { ushort newMax, newMin; if (rangeAttribute != null && rangeAttribute.Maximum is ushort rMax && rangeAttribute.Minimum is ushort rMin) { newMax = rMax; newMin = rMin; } else { newMax = ushort.MaxValue; newMin = ushort.MinValue; } valueControl = new MahApps.Metro.Controls.NumericUpDown() { Maximum = newMax, Minimum = newMin, ParsingNumberStyle = System.Globalization.NumberStyles.Integer, HideUpDownButtons = true }; (valueControl as MahApps.Metro.Controls.NumericUpDown).SetBinding(MahApps.Metro.Controls.NumericUpDown.ValueProperty, propName); if (assetPickerAttribute != null) { var vcMenu = new ContextMenu(); vcMenu.Items.Add(ContextHelper.CreateSelectAssetButton(assetPickerAttribute.AssetType, (asset) => { (valueControl as MahApps.Metro.Controls.NumericUpDown).Value = asset.id; }, assetPickerAttribute.Key, assetPickerAttribute.Icon)); (valueControl as MahApps.Metro.Controls.NumericUpDown).ContextMenu = vcMenu; } }