protected virtual FrameworkElement CreateComboBox(PropertyItem pi, PropertyControlFactoryOptions options) { var cbox = new ComboBox() { IsEditable = true }; cbox.SetBinding(Selector.SelectedItemProperty, new Binding(pi.Descriptor.Name + ".Text")); cbox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding(pi.Descriptor.Name + ".Values")); return cbox; }
private StackPanel build_combo(string label_name, string prop_names, string selected_prop) { TextBlock tb_materials = new TextBlock(); tb_materials.Text = label_name; tb_materials.TextAlignment = TextAlignment.Right; tb_materials.Width = 80; tb_materials.Height = 20; ComboBox cb_materials = new ComboBox(); cb_materials.Width = 190; cb_materials.Height = 20; cb_materials.Foreground = Brushes.White; Binding bind = new Binding(prop_names); cb_materials.SetBinding(ComboBox.ItemsSourceProperty, bind); Binding bind2 = new Binding(selected_prop); cb_materials.SetBinding(ComboBox.SelectedItemProperty, bind2); StackPanel sp_materials = new StackPanel(); sp_materials.Orientation = Orientation.Horizontal; sp_materials.Height = 25; sp_materials.Children.Add(tb_materials); sp_materials.Children.Add(cb_materials); return sp_materials; }
public void SetupCustomUIElements(dynNodeView nodeUI) { // Do not call 'NodeModel.InitializeUI' here since it will cause // that method to dispatch the call back to 'SetupCustomUIElements' // method, resulting in an eventual stack overflow. // // base.InitializeUI(nodeUI); //add a drop down list to the window var combo = new ComboBox { Width = System.Double.NaN, MinWidth = 100, Height = Configurations.PortHeightInPixels, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Center }; nodeUI.inputGrid.Children.Add(combo); Grid.SetColumn(combo, 0); Grid.SetRow(combo, 0); combo.DropDownOpened += combo_DropDownOpened; combo.SelectionChanged += delegate { if (combo.SelectedIndex != -1) { RequiresRecalc = true; } }; combo.DropDownClosed += delegate { //disallow selection of nothing if (combo.SelectedIndex == -1) { SelectedIndex = 0; } }; combo.DataContext = this; //bind this combo box to the selected item hash var bindingVal = new Binding("Items") { Mode = BindingMode.TwoWay, Source = this }; combo.SetBinding(ItemsControl.ItemsSourceProperty, bindingVal); //bind the selected index to the var indexBinding = new Binding("SelectedIndex") { Mode = BindingMode.TwoWay, Source = this }; combo.SetBinding(Selector.SelectedIndexProperty, indexBinding); }
private void build_gui() { this.DataContext = shapes; TextBlock tb_shapes = new TextBlock(); tb_shapes.Text = " Shapes: "; tb_shapes.Width = 60; tb_shapes.Height = 20; ComboBox cb = new ComboBox(); cb.Width = 210; cb.Height = 20; cb.Foreground = Brushes.White; Binding bind = new Binding("ShapeNames"); cb.SetBinding(ComboBox.ItemsSourceProperty, bind); Binding bind2 = new Binding("SelectedShape"); cb.SetBinding(ComboBox.SelectedItemProperty, bind2); StackPanel sp = new StackPanel(); sp.Orientation = Orientation.Horizontal; sp.Height = 25; sp.Children.Add(tb_shapes); sp.Children.Add(cb); TextBlock tb_materials = new TextBlock(); tb_materials.Text = " Materials: "; tb_materials.Width = 60; ComboBox cb_mat = new ComboBox(); cb_mat.Width = 210; cb_mat.Height = 20; cb_mat.Foreground = Brushes.White; Binding bind3 = new Binding("MaterialNames"); cb_mat.SetBinding(ComboBox.ItemsSourceProperty, bind3); Binding bind4 = new Binding("SelectedMaterial"); cb_mat.SetBinding(ComboBox.SelectedItemProperty, bind4); StackPanel sp_mat = new StackPanel(); sp_mat.Orientation = Orientation.Horizontal; sp_mat.Children.Add(tb_materials); sp_mat.Children.Add(cb_mat); StackPanel all = new StackPanel(); all.Children.Add(sp); all.Children.Add(sp_mat); Expander expander = new Expander(); expander.Header = " Shapes"; expander.Foreground = Brushes.White; expander.Content = all; this.Content = expander; }
private void build_gui() { this.DataContext = tm_operators; TextBlock tb_operators = new TextBlock(); tb_operators.Text = " Operators: "; tb_operators.Width = 60; tb_operators.Height = 20; ComboBox cb = new ComboBox(); cb.Width = 210; cb.Height = 20; cb.Foreground = Brushes.White; Binding bind = new Binding("OperatorNames"); cb.SetBinding(ComboBox.ItemsSourceProperty, bind); Binding bind2 = new Binding("SelectedOperator"); cb.SetBinding(ComboBox.SelectedItemProperty, bind2); StackPanel sp = new StackPanel(); sp.Orientation = Orientation.Horizontal; sp.Height = 25; sp.Children.Add(tb_operators); sp.Children.Add(cb); StackPanel tonemap_sp = this.build_lblcb(" Tone Mapping: ", "ToneMapping"); StackPanel sp_operator = new StackPanel(); if (tm_operators.SelectedOperator == "Reinhard") { this.build_reinhard(sp_operator); } else if (tm_operators.SelectedOperator == "Photoreceptor") { this.build_photoreceptor(sp_operator); } else { } StackPanel all = new StackPanel(); all.Children.Add(tonemap_sp); all.Children.Add(sp); all.Children.Add(sp_operator); Expander expander = new Expander(); expander.Header = " Tone mapping"; expander.Foreground = Brushes.White; expander.Content = all; this.Content = expander; }
public override FrameworkElement GenerateUiElement(string Key, ArmA_UI_Editor.UI.Snaps.EditingSnap window, PTypeDataTag tag) { var cb = new ComboBox(); cb.Tag = tag; cb.DisplayMemberPath = "Name"; cb.SelectedValuePath = "Value"; cb.ItemsSource = this.Items; var binding = new Binding("Value"); binding.Source = AddInManager.Instance.MainFile; binding.NotifyOnSourceUpdated = true; if (tag.PropertyObject is SqfProperty) { binding.Converter = new SqfPropertyConverter(Key, tag, this.Items); binding.ConverterParameter = tag; } else { binding.Converter = new NormalPropertyConverter(Key, this.Items); } cb.SetBinding(ComboBox.SelectedIndexProperty, binding); cb.SourceUpdated += Cb_SourceUpdated; cb.ToolTip = App.Current.Resources["STR_CODE_Property_ValueList"] as String; return cb; }
public override void SetupCustomUIElements(object ui) { var nodeUI = ui as dynNodeView; base.SetupCustomUIElements(nodeUI); //add a drop down list to the window var combo = new ComboBox { Width = 300, HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Center }; nodeUI.inputGrid.Children.Add(combo); System.Windows.Controls.Grid.SetColumn(combo, 0); System.Windows.Controls.Grid.SetRow(combo, 0); combo.DropDownOpened += new EventHandler(combo_DropDownOpened); combo.SelectionChanged += delegate { if (combo.SelectedIndex != -1) { RequiresRecalc = true; } }; combo.DataContext = this; //bind this combo box to the selected item hash var bindingVal = new System.Windows.Data.Binding("Items") { Mode = BindingMode.TwoWay, Source = this }; combo.SetBinding(ComboBox.ItemsSourceProperty, bindingVal); //bind the selected index to the var indexBinding = new System.Windows.Data.Binding("SelectedIndex") { Mode = BindingMode.TwoWay, Source = this }; combo.SetBinding(ComboBox.SelectedIndexProperty, indexBinding); }
private async void GenerateFormItems() { if (_labels == null) { _labels = await _labelGenerator.FetchAllLabelTemplates(); } if (_labels.HasContent()) { StackPanel stackPanel; foreach (var label in _labels) { stackPanel = new StackPanel { Orientation = Orientation.Horizontal }; stackPanel.Children.Add(new Label { Content = label.FriendlyName }); var comboBox = new ComboBox {Name = label.Name}; comboBox.SetBinding( ItemsControl.ItemsSourceProperty, new Binding { Source = GetInstalledPrinters() }); comboBox.SelectedValue = label.Printer; comboBox.SetBinding( Selector.SelectedItemProperty, new Binding("Printer") { Source = label, Mode = BindingMode.TwoWay }); stackPanel.Children.Add(comboBox); SpLabels.Children.Add(stackPanel); } stackPanel = new StackPanel { Orientation = Orientation.Horizontal }; var saveButton = new Button { Name = "Save", Content = "Save" }; saveButton.Click += SaveButtonOnClick; stackPanel.Children.Add(saveButton); SpLabels.Children.Add(stackPanel); } }
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { var editElement = new ComboBox(); editElement.SetBinding(Selector.SelectedItemProperty, Binding); var prop = dataItem.GetType().GetProperty(Binding.Path.Path, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public); Utilities.FillEnums(editElement, prop.PropertyType); return editElement; }
public ComboBoxEditor(WorkFrame frame, object[] items) : base(frame) { ComboBox comboBox = new ComboBox(); comboBox.DataContext = this; comboBox.ItemsSource = items; var binding = new Binding("Value"); binding.Mode = BindingMode.TwoWay; comboBox.SetBinding(ComboBox.SelectedItemProperty, binding); Content = comboBox; }
private void SetSignalTowerBinding(SignalTowerConfig config, ComboBox red, ComboBox buzzer) { Binding bd = new Binding("RedLamp"); bd.Source = config; bd.Mode = BindingMode.TwoWay; red.SetBinding(ComboBox.SelectedIndexProperty, bd); bd = new Binding("Buzzer"); bd.Source = config; bd.Mode = BindingMode.TwoWay; buzzer.SetBinding(ComboBox.SelectedIndexProperty, bd); }
public FrameworkElement CreateView(PropertyInfo property) { var inputControl = new ComboBox(); var enumtype = property.PropertyType; inputControl.ItemsSource = GetNameList(enumtype); var binding = new Binding(property.Name); binding.Mode = BindingMode.TwoWay; inputControl.SetBinding(ComboBox.SelectedItemProperty, binding); return inputControl; }
public override FrameworkElement LoadUI(Option option) { var box = new ComboBox { MinWidth = 100, DataContext = this, ItemsSource = Items }; box.SetBinding(Selector.SelectedItemProperty, new Binding(nameof(CurrentItem))); return box; }
public void DataBind(ComboBox lbl, string properySourcePath, string selectedValuePath, Hashtable propertyBag) { //binding the list string key = properySourcePath.Split('.')[0]; lbl.ItemsSource = (IEnumerable)propertyBag[key]; //binding selected value key = selectedValuePath.Split('.')[0]; string path = selectedValuePath.Replace(key + ".", ""); Binding selectedValue = new Binding(path); selectedValue.Source = propertyBag[key]; lbl.SetBinding(ComboBox.SelectedItemProperty, selectedValue); }
public FrameworkElement LoadUI(Option option) { var box = new ComboBox { MinWidth = 100, DataContext = this, ItemsSource = Profiles }; box.SetBinding(Selector.SelectedItemProperty, new Binding("CurrentEncoder")); return box; }
public void build_gui() { this.DataContext = lights; TextBlock tb_lights = new TextBlock(); tb_lights.Text = " Lights: "; ComboBox cb = new ComboBox(); cb.Width = 150; cb.Foreground = Brushes.White; Binding bind = new Binding("LightNames"); cb.SetBinding(ComboBox.ItemsSourceProperty, bind); Binding bind2 = new Binding("SelectedLight"); cb.SetBinding(ComboBox.SelectedItemProperty, bind2); StackPanel sp = new StackPanel(); sp.Orientation = Orientation.Horizontal; sp.Children.Add(tb_lights); sp.Children.Add(cb); StackPanel all = new StackPanel(); all.Children.Add(sp); if (this.lights.SelectedLight != "") { if (this.lights.LightType == "PointLight") { this.build_point_light(all); } } Expander expander = new Expander(); expander.Header = "Lights"; expander.Foreground = Brushes.White; expander.Content = all; this.Content = expander; }
public FrameworkElement CreateControlForLookup(FormItemContext context, List<NameValuePair> options) { ComboBox cb = new ComboBox(); cb.ItemsSource = options; cb.SelectedValuePath = "Value"; var binding = new Binding(context.PropertyInfo.Name) { Mode = BindingMode.TwoWay, }; binding.ValidationRules.Add(new AnnotationValidationRule(context.PropertyInfo)); cb.SetBinding(ComboBox.SelectedValueProperty, binding); CustomValidation.SetValidationProperty(cb, ComboBox.SelectedValueProperty); StyleHelper.ApplyStyle(cb, FormControlConstrants.EDIT_COMBO_STYLE); return cb; }
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { var contentPresenter = new ContentPresenter(); Dispatcher.BeginInvoke((Action)(() => { if ((dataItem.GetType().GetProperty(this.BindingPath) != null)) { if (this.SupportedValues != null) { var comboBox = new ComboBox(); comboBox.IsHitTestVisible = false; var itemsSourceBinding = new Binding("SupportedValues") { Source = this }; comboBox.SetBinding(ComboBox.ItemsSourceProperty, itemsSourceBinding); comboBox.DisplayMemberPath = this.SupportedValuesDisplayMemberPath; comboBox.SelectedValuePath = this.SupportedValuesValuePath; comboBox.IsSynchronizedWithCurrentItem = false; comboBox.SetBinding(ComboBox.SelectedValueProperty, new Binding(this.BindingPath) { Source = dataItem, Mode = BindingMode.TwoWay }); contentPresenter.Content = comboBox; } else { string typeName = dataItem.GetType().GetProperty(this.BindingPath).PropertyType.FullName; switch (typeName) { case "System.Int32": case "System.Int64": case "System.String": { var textBlock = new TextBlock(); textBlock.SetBinding(TextBlock.TextProperty, new Binding(this.BindingPath) { Source = dataItem }); contentPresenter.Content = textBlock; break; } case "System.Boolean": { var checkBox = new CheckBox(); checkBox.SetBinding(CheckBox.IsCheckedProperty, new Binding(this.BindingPath) { Source = dataItem }); checkBox.IsEnabled = false; contentPresenter.Content = checkBox; break; } } } } })); return contentPresenter; }
private Control GenerateComboBox(Type t, PropertyInfo property, Binding binding) { ComboBox comboBox = new ComboBox() { Margin = new Thickness(0, 2, 18, 2) }; comboBox.ItemsSource = Enum.GetValues(t); #if !SILVERLIGHT comboBox.IsReadOnly = !(bindables[property.Name].Direction == BindingDirection.TwoWay); #else comboBox.IsEnabled = (bindables[property.Name].Direction == BindingDirection.TwoWay); #endif this.bindings.Add(property.Name, comboBox.SetBinding(ComboBox.SelectedItemProperty, binding)); return(comboBox); }
public override FrameworkElement CreateControl(FormItemContext context) { ComboBox cb = new ComboBox(); var enumtype = context.PropertyInfo.PropertyType; var source = EnumNameValuePair.EnumToList(enumtype); cb.ItemsSource = source; cb.SelectedValuePath = "Value"; var binding = new Binding(context.PropertyInfo.Name) { Mode = BindingMode.TwoWay, }; binding.ValidationRules.Add(new AnnotationValidationRule(context.PropertyInfo)); cb.SetBinding(ComboBox.SelectedValueProperty, binding); CustomValidation.SetValidationProperty(cb, ComboBox.SelectedValueProperty); StyleHelper.ApplyStyle(cb, FormControlConstrants.EDIT_COMBO_STYLE); return cb; }
public void ShouldInitiallySetSelectedItemInSelectorFromModel() { var model = new MockModel(); var item1 = new MockItem { Property = 1 }; var item2 = new MockItem { Property = 2 }; var itemsSource = new List<MockItem> { item1, item2 }; var selector = new ComboBox(); selector.ItemsSource = itemsSource; SelectorExtensions.SetSelectedValuePath(selector, "Property"); model.SelectedValueInModel = 2; Binding valueBinding = new Binding("SelectedValueInModel"); valueBinding.Mode = BindingMode.TwoWay; valueBinding.Source = model; selector.SetBinding(SelectorExtensions.SelectedValueProperty, valueBinding); Assert.AreSame(item2, selector.SelectedItem); }
public void ShouldUpdateModelOnSelectionChange() { var model = new MockModel(); var item1 = new MockItem { Property = 1 }; var item2 = new MockItem { Property = 2 }; var itemsSource = new List<MockItem> { item1, item2 }; var selector = new ComboBox(); selector.ItemsSource = itemsSource; SelectorExtensions.SetSelectedValuePath(selector, "Property"); Binding valueBinding = new Binding("SelectedValueInModel"); valueBinding.Mode = BindingMode.TwoWay; valueBinding.Source = model; selector.SetBinding(SelectorExtensions.SelectedValueProperty, valueBinding); selector.SelectedItem = item1; Assert.AreEqual(item1.Property, model.SelectedValueInModel); selector.SelectedItem = item2; Assert.AreEqual(item2.Property, model.SelectedValueInModel); }
public override void OnApplyTemplate() { base.OnApplyTemplate(); _pageSizeComboBox = GetTemplateChild(CustomDataPager.PageSizeComboBox) as ComboBox; _totalCountTextBlock = GetTemplateChild(CustomDataPager.TotalCountTextBlock) as TextBlock; if (_pageSizeComboBox != null) { _pageSizeComboBox.ItemsSource = new List<int>(new int[] { 1, 5, 10, 20, 30, 50, 100 }); var binding = new Binding("Source.PageSize"); binding.Mode = BindingMode.TwoWay; binding.Source = this; _pageSizeComboBox.SetBinding(ComboBox.SelectedValueProperty, binding); } if (_totalCountTextBlock != null) { var binding = new Binding("Source.TotalItemCount"); binding.Source = this; _totalCountTextBlock.SetBinding(TextBlock.TextProperty, binding); } }
public void InvalidValuePathThrows() { var model = new MockModel(); var item1 = new MockItem { Property = 1 }; var itemsSource = new List<MockItem> { item1 }; var selector = new ComboBox(); selector.ItemsSource = itemsSource; Binding valueBinding = new Binding("SelectedValueInModel"); valueBinding.Mode = BindingMode.TwoWay; valueBinding.Source = model; selector.SetBinding(SelectorExtensions.SelectedValueProperty, valueBinding); SelectorExtensions.SetSelectedValuePath(selector, "InvalidProperty"); }
public void ShouldNotFailIfItemsSourceIsNotSetBeforeSettingTheBindingToModel() { var model = new MockModel(); var item1 = new MockItem { Property = 1 }; var item2 = new MockItem { Property = 2 }; var itemsSource = new List<MockItem> { item1, item2 }; var selector = new ComboBox(); SelectorExtensions.SetSelectedValuePath(selector, "Property"); Binding valueBinding = new Binding("SelectedValueInModel"); valueBinding.Mode = BindingMode.TwoWay; valueBinding.Source = model; selector.SetBinding(SelectorExtensions.SelectedValueProperty, valueBinding); selector.ItemsSource = itemsSource; }
void InitializeOperandType(InstructionOperandVM instructionOperandVM) { switch (instructionOperandVM.InstructionOperandType) { case InstructionOperandType.None: Content = null; break; case InstructionOperandType.SByte: case InstructionOperandType.Byte: case InstructionOperandType.Int32: case InstructionOperandType.Int64: case InstructionOperandType.Single: case InstructionOperandType.Double: case InstructionOperandType.String: // Don't cache the TextBox as a field in this class. The error border disappears when // switching from a textbox opcode to a non-textbox opcode and back to the textbox // again. Only solution seems to be to create a new textbox. var textBox = Content as TextBox; if (textBox == null) { textBox = new TextBox(); var binding = new Binding("InstructionOperandVM.Text.StringValue") { Source = this, ValidatesOnDataErrors = true, ValidatesOnExceptions = true, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }; textBox.SetBinding(TextBox.TextProperty, binding); binding = new Binding("TextBoxStyle") { Source = this, }; textBox.SetBinding(TextBox.StyleProperty, binding); Content = textBox; } break; case InstructionOperandType.Field: case InstructionOperandType.Method: case InstructionOperandType.Token: case InstructionOperandType.Type: case InstructionOperandType.MethodSig: case InstructionOperandType.SwitchTargets: var button = Content as FastClickButton; if (button == null) { button = new FastClickButton(); var binding = new Binding("InstructionOperandVM.Other") { Source = this, Mode = BindingMode.OneWay, Converter = CilObjectConverter.Instance, ValidatesOnDataErrors = true, ValidatesOnExceptions = true, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }; button.SetBinding(Button.ContentProperty, binding); binding = new Binding("ButtonStyle") { Source = this, }; button.SetBinding(Button.StyleProperty, binding); binding = new Binding("ButtonCommand") { Source = this, }; button.SetBinding(Button.CommandProperty, binding); button.CommandParameter = button; Content = button; } break; case InstructionOperandType.BranchTarget: case InstructionOperandType.Local: case InstructionOperandType.Parameter: var comboBox = Content as ComboBox; if (comboBox == null) { comboBox = new ComboBox(); comboBox.ItemTemplate = (DataTemplate)GetValue(ComboBoxItemTemplateProperty); ComboBoxAttachedProps.SetSelectionBoxItemTemplate(comboBox, (DataTemplate)GetValue(ComboBoxSelectionBoxItemTemplateProperty)); var binding = new Binding("InstructionOperandVM.OperandListVM.Items") { Source = this, }; comboBox.SetBinding(ComboBox.ItemsSourceProperty, binding); binding = new Binding("InstructionOperandVM.OperandListVM.SelectedIndex") { Source = this, ValidatesOnDataErrors = true, ValidatesOnExceptions = true, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }; comboBox.SetBinding(ComboBox.SelectedIndexProperty, binding); binding = new Binding("ComboBoxStyle") { Source = this, }; comboBox.SetBinding(ComboBox.StyleProperty, binding); binding = new Binding("ComboBoxItemTemplate") { Source = this, }; comboBox.SetBinding(ComboBox.ItemTemplateProperty, binding); binding = new Binding("ComboBoxSelectionBoxItemTemplate") { Source = this, }; comboBox.SetBinding(ComboBoxAttachedProps.SelectionBoxItemTemplateProperty, binding); Content = comboBox; } break; default: throw new InvalidOperationException(); } }
private static ModelPropertyUiInfo CreateComboBoxField(Grid parent, DisplayPropertyInfo property, String bindingPath, Style style, int row, int column) { Label labelElement = CreateLabel(property, row, column); var comboBox = new ComboBox { Name = "comboBox" + property.PropertyName, }; if (style != null) { comboBox.Style = style; } comboBox.SetBinding(Selector.SelectedItemProperty, ModelUiCreatorHelper.CreateBinding(property, bindingPath)); if (property.IsReadOnly) { comboBox.IsReadOnly = true; } ObjectDataProvider provider = new ObjectDataProvider() { ObjectType = typeof(Enum), MethodName = "GetValues", }; provider.MethodParameters.Add(property.PropertyInfo.PropertyType); BindingOperations.SetBinding(comboBox, ItemsControl.ItemsSourceProperty, new Binding() { Source = provider }); Grid.SetRow(comboBox, row); Grid.SetColumn(comboBox, checked(column + 1)); parent.Children.Add(labelElement); parent.Children.Add(comboBox); // return ModelPropertyUiInfo elememtsInfo = new ModelPropertyUiInfo(property); elememtsInfo.Label = labelElement; elememtsInfo.Content = comboBox; return elememtsInfo; }
private static ComboBox CreateComboBoxForEnumObject(object obj, string propertyName) { ComboBox comboBox = new ComboBox(); foreach (string name in obj.GetType().GetProperty(propertyName).PropertyType.GetEnumNames()) { comboBox.Items.Add(name); } Binding bd = new Binding(propertyName); bd.Source = obj; if (obj.GetType().GetProperty(propertyName).GetSetMethod(false) == null) bd.Mode = BindingMode.OneWay; else bd.Mode = BindingMode.TwoWay; comboBox.SetBinding(ComboBox.TextProperty, bd); return comboBox; }
//设置图元名称显示位置 private void AddNamePosition(DesignerContainer di) { var tbTag = di.Template.FindName("tbTag", di) as TextBlock; var nameInfo = new string[3]; var cbHA = new ComboBox(); cbHA.MaxWidth = 80; cbHA.Items.Add(new ComboBoxItem { Content = "Left", Tag = "Left" }); cbHA.Items.Add(new ComboBoxItem { Content = "Center", Tag = "Center" }); cbHA.Items.Add(new ComboBoxItem { Content = "Right", Tag = "Right" }); var bdHA = new Binding("HorizontalAlignment") { Source = tbTag }; bdHA.Mode = BindingMode.TwoWay; bdHA.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; cbHA.SetBinding(ComboBox.TextProperty, bdHA); if (cbHA.SelectedItem == null) { cbHA.SelectedIndex = 1; } nameInfo[0] = (cbHA.SelectedItem as ComboBoxItem).Content.ToString(); cbHA.SelectionChanged += (sender, e) => { nameInfo[0] = (cbHA.SelectedItem as ComboBoxItem).Content.ToString(); di.NamePosition = ArrToStr(nameInfo); }; if (di.NamePosition.Split('|').Length > 2) { foreach (ComboBoxItem item in cbHA.Items) { if (item.Content.ToString().Equals(di.NamePosition.Split('|')[0])) { item.IsSelected = true; break; } } } AddProItem("名称水平", cbHA); var cbVA = new ComboBox(); cbVA.MaxWidth = 80; cbVA.Items.Add(new ComboBoxItem { Content = "Top", Tag = "Top" }); cbVA.Items.Add(new ComboBoxItem { Content = "Center", Tag = "Center" }); cbVA.Items.Add(new ComboBoxItem { Content = "Bottom", Tag = "Bottom" }); var bdVA = new Binding("VerticalAlignment") { Source = tbTag }; bdVA.Mode = BindingMode.TwoWay; bdVA.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; cbVA.SetBinding(ComboBox.TextProperty, bdVA); if (cbVA.SelectedItem == null) { cbVA.SelectedIndex = 2; } nameInfo[1] = (cbVA.SelectedItem as ComboBoxItem).Content.ToString(); cbVA.SelectionChanged += (sender, e) => { nameInfo[1] = (cbVA.SelectedItem as ComboBoxItem).Content.ToString(); di.NamePosition = ArrToStr(nameInfo); }; if (di.NamePosition.Split('|').Length > 2) { foreach (ComboBoxItem item in cbVA.Items) { if (item.Content.ToString().Equals(di.NamePosition.Split('|')[1])) { item.IsSelected = true; break; } } } AddProItem("名称垂直", cbVA); var tbOffset = new TextBox(); var bdOffset = new Binding("Margin") { Source = tbTag }; bdOffset.Mode = BindingMode.TwoWay; bdOffset.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; tbOffset.SetBinding(TextBox.TextProperty, bdOffset); if (string.IsNullOrEmpty(tbOffset.Text)) { tbOffset.Text = "-100,-20,-100,-20"; } if (tbOffset.Text == "-20,-20,-20,-20") { tbOffset.Text = "-100,-20,-100,-20"; } nameInfo[2] = tbOffset.Text; tbOffset.TextChanged += (sender, e) => { nameInfo[2] = tbOffset.Text; di.NamePosition = ArrToStr(nameInfo); }; AddProItem("名称偏移", tbOffset); }
/// <summary> /// When overridden in a derived class, gets an editing element that is bound to the column's <see cref="P:System.Windows.Controls.DataGridBoundColumn.Binding"/> property value. /// </summary> /// <param name="cell">The cell that will contain the generated element.</param> /// <param name="dataItem">The data item represented by the row that contains the intended cell.</param> /// <returns> /// A new editing element that is bound to the column's <see cref="P:System.Windows.Controls.DataGridBoundColumn.Binding"/> property value. /// </returns> protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { #if !SILVERLIGHT _isEditing = false; #endif if(!string.IsNullOrEmpty(Field)) { var codedValueSources = Utilities.DynamicCodedValueSource.GetCodedValueSources(LookupField, FieldInfo, dataItem, DynamicCodedValueSource, nullableSources); if (codedValueSources != null) { ComboBox box = new ComboBox { Margin = new Thickness(4.0), VerticalAlignment = VerticalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, DisplayMemberPath = "DisplayName" }; if (!string.IsNullOrEmpty(LookupField) && DynamicCodedValueSource != null) { // Item Source Binding lookupConverter.LookupField = this.LookupField; lookupConverter.Field = this.FieldInfo; lookupConverter.NullableSources = this.nullableSources; Binding binding = new Binding(); binding.Mode = BindingMode.OneWay; binding.Converter = lookupConverter; binding.ConverterParameter = DynamicCodedValueSource; box.SetBinding(ComboBox.ItemsSourceProperty, binding); // Selected Item Binding selectedConverter.Field = Field; selectedConverter.LookupField = LookupField; selectedConverter.NullableSources = this.nullableSources; Binding selectedBinding = new Binding(); selectedBinding.Mode = BindingMode.OneWay; selectedBinding.Converter = selectedConverter; selectedBinding.ConverterParameter = this.DynamicCodedValueSource; box.SetBinding(ComboBox.SelectedItemProperty, selectedBinding); box.SelectionChanged += box_SelectionChanged; } return box; } else if(FieldInfo.Type == Client.Field.FieldType.Date) { DateTimePicker dtp = new DateTimePicker { Margin = new Thickness(4.0), VerticalAlignment = VerticalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, DateTimeFormat = this.DateTimeFormat, DateTimeKind = this.DateTimeKind, Language = cell.Language }; Binding selectedBinding = #if SILVERLIGHT new Binding(Field); #else new Binding("Attributes["+Field+"]"); if (FieldDomainUtils.IsDynamicDomain(FieldInfo, LayerInfo)) { selectedBinding.ValidationRules.Add(new DynamicRangeDomainValidationRule() { ValidationStep = ValidationStep.ConvertedProposedValue, Field = FieldInfo, LayerInfo = LayerInfo, Graphic = dataItem as Graphic }); } #endif selectedBinding.Mode = BindingMode.TwoWay; selectedBinding.ValidatesOnExceptions = true; selectedBinding.NotifyOnValidationError = true; dtp.SetBinding(DateTimePicker.SelectedDateProperty, selectedBinding); return dtp; } else { TextBox box = new TextBox { Margin = new Thickness(4.0), VerticalAlignment = VerticalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, }; box.MaxLength = Field.Length; Binding binding = #if SILVERLIGHT new Binding(Field); binding.Mode = BindingMode.TwoWay; binding.ValidatesOnExceptions = true; binding.NotifyOnValidationError = true; binding.Converter = emptyStringToNullConverter; #else new Binding("Attributes["+Field+"]"); binding.Mode = BindingMode.TwoWay; stringToPrimitiveTypeConverter.FieldType = FieldInfo.Type; binding.Converter = stringToPrimitiveTypeConverter; // Validates that the value entered into the text box can be // converted to the corrected field type without error. // if value cannot be converted to the correct type validation // error will be triggered based on binding trigger below. binding.ValidationRules.Add(new FeatureValidationRule() { ValidationStep = ValidationStep.ConvertedProposedValue, FieldType = FieldInfo.Type, Nullable = FieldInfo.Nullable }); if (FieldDomainUtils.IsDynamicDomain(FieldInfo, LayerInfo)) { binding.ValidationRules.Add(new DynamicRangeDomainValidationRule() { ValidationStep = ValidationStep.ConvertedProposedValue, Field = FieldInfo, LayerInfo = LayerInfo, Graphic = dataItem as Graphic }); } // Build a data trigger to show the validation error i.e. red outline around textbox // with message content in tooltip. var setterBinding = new Binding("(Validation.Errors)[0].ErrorContent"); setterBinding.RelativeSource = RelativeSource.Self; var setter = new Setter(); setter.Property = TextBox.ToolTipProperty; setter.Value = setterBinding; var trigger = new Trigger(); trigger.Property = Validation.HasErrorProperty; trigger.Value = true; trigger.Setters.Add(setter); var style = new Style(typeof(TextBox)); style.Triggers.Add(trigger); box.Style = style; #endif box.SetBinding(TextBox.TextProperty, binding); return box; } } return new TextBlock(); // Should never reach this line. }
/// <summary> /// When overridden in a derived class, gets an editing element that is bound to the column's <see cref="P:System.Windows.Controls.DataGridBoundColumn.Binding"/> property value. /// </summary> /// <param name="cell">The cell that will contain the generated element.</param> /// <param name="dataItem">The data item represented by the row that contains the intended cell.</param> /// <returns> /// A new editing element that is bound to the column's <see cref="P:System.Windows.Controls.DataGridBoundColumn.Binding"/> property value. /// </returns> protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { #if !SILVERLIGHT _isEditing = false; #endif ComboBox box = new ComboBox() { Margin = new Thickness(4.0), VerticalAlignment = VerticalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, DisplayMemberPath = "DisplayName" }; if (!string.IsNullOrEmpty(Field) && this.CodedValueSources != null) { // Item Source Binding Binding binding = new Binding(); binding.Mode = BindingMode.OneWay; binding.Converter = lookupConverter; binding.ConverterParameter = this.CodedValueSources; box.SetBinding(ComboBox.ItemsSourceProperty, binding); // Selected Item Binding selectedConverter.Field = Field; Binding selectedBinding = new Binding(); selectedBinding.Mode = BindingMode.OneWay; selectedBinding.Converter = selectedConverter; selectedBinding.ConverterParameter = this.CodedValueSources; box.SetBinding(ComboBox.SelectedItemProperty, selectedBinding); box.SelectionChanged += box_SelectionChanged; } return box; }
/// <summary> /// Creates the combo box. /// </summary> /// <param name="d">The d.</param> /// <returns></returns> protected virtual FrameworkElement CreateComboBox(PropertyDefinition d) { var c = new ComboBox { IsEditable = d.IsEditable, Focusable = false, Margin = new Thickness(0, 0, -1, -1) }; if (d.ItemsSource != null) c.ItemsSource = d.ItemsSource; else { if (d.ItemsSourceProperty != null) c.SetBinding(ItemsControl.ItemsSourceProperty, new Binding(d.ItemsSourceProperty)); } c.SetBinding(d.IsEditable ? ComboBox.TextProperty : Selector.SelectedValueProperty, d.CreateBinding()); return c; }
private void AddItem(string name, object value, string bindingName) { Func<ComboBoxItem, bool> predicate = null; var type = value.GetType(); var element = new Grid { Margin = new Thickness(0.0, 0.0, 0.0, 10.0) }; var definition4 = new ColumnDefinition { Width = new GridLength(126.0) }; element.ColumnDefinitions.Add(definition4); var definition5 = new ColumnDefinition { Width = new GridLength(1.0, GridUnitType.Star) }; element.ColumnDefinitions.Add(definition5); var block = new TextBlock { Text = name, Foreground = Brushes.White }; element.Children.Add(block); if (((type == typeof(double)) || (type == typeof(int))) || ((type == typeof(float)) || (type == typeof(Thickness)))) { var binding = new Binding(bindingName) { Mode = BindingMode.TwoWay }; var textBox = new TextBox(); textBox.TextChanged += delegate { foreach (var ch in textBox.Text) { switch (ch) { case ',': case '.': return; } } textBox.Text = new string(( from c in textBox.Text where char.IsDigit(c) select c ).ToArray()); textBox.SelectionStart = textBox.Text.Length; }; textBox.Text = value.ToString(); Grid.SetColumn(textBox, 1); textBox.SetBinding(TextBox.TextProperty, binding); element.Children.Add(textBox); } else if (type == typeof(SolidColorBrush)) { var binding2 = new Binding(bindingName) { Mode = BindingMode.TwoWay, Converter = Resources["brushToColorConverter"] as Legacy.BrushToColorConverter }; var box = new ColorComboBox { Margin = new Thickness(0.0, -4.0, 0.0, 4.0) }; Grid.SetColumn(box, 1); box.SetBinding(ColorComboBox.SelectedColorProperty, binding2); element.Children.Add(box); } else { if (type == typeof(string)) { var grid2 = new Grid { Margin = new Thickness(0.0, 0.0, 0.0, 10.0) }; var definition = new ColumnDefinition { Width = new GridLength(0.3, GridUnitType.Star) }; grid2.ColumnDefinitions.Add(definition); var definition2 = new ColumnDefinition { Width = new GridLength(1.0, GridUnitType.Auto) }; grid2.ColumnDefinitions.Add(definition2); var definition3 = new ColumnDefinition { Width = new GridLength(0.3, GridUnitType.Star) }; grid2.ColumnDefinitions.Add(definition3); var grid3 = new Grid { Height = 1.0, Background = Brushes.White }; Grid.SetColumn(grid3, 0); var grid4 = new Grid { Height = 1.0, Background = Brushes.White }; Grid.SetColumn(grid4, 2); var block2 = new TextBlock { Text = value.ToString(), Foreground = Brushes.White, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(5.0, 0.0, 5.0, 0.0) }; Grid.SetColumn(block2, 1); grid2.Children.Add(grid3); grid2.Children.Add(grid4); grid2.Children.Add(block2); spEditor.Children.Add(grid2); return; } if (type == typeof(FontFamily)) { var binding3 = new Binding(bindingName) { Mode = BindingMode.TwoWay }; var list = (from f in new InstalledFontCollection().Families select new ComboBoxItem { Content = f.Name, HorizontalContentAlignment = HorizontalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Stretch, FontFamily = new FontFamily(f.Name) }).ToList<ComboBoxItem>(); var combo = new ComboBox { ItemsSource = list }; combo.SetBinding(FontFamilyProperty, binding3); combo.SelectionChanged += delegate { if (combo.SelectedItem != null) { var selectedItem = (ComboBoxItem)combo.SelectedItem; combo.FontFamily = selectedItem.FontFamily; } }; predicate = c => c.FontFamily.ToString() == value.ToString(); combo.SelectedItem = combo.Items.Cast<ComboBoxItem>().FirstOrDefault<ComboBoxItem>(predicate); Grid.SetColumn(combo, 1); element.Children.Add(combo); } else if (type == typeof(FontWeight)) { var binding4 = new Binding(bindingName) { Mode = BindingMode.TwoWay }; var combo = new ComboBox { ItemsSource = Resources["fontWeights"] as FontWeight[] }; combo.SetBinding(FontWeightProperty, binding4); combo.SelectionChanged += delegate { if (combo.SelectedItem != null) { combo.FontWeight = (FontWeight)combo.SelectedItem; } }; combo.SelectedItem = (FontWeight)value; Grid.SetColumn(combo, 1); element.Children.Add(combo); } } spEditor.Children.Add(element); }
/// <summary> /// Creates a combo box. /// </summary> /// <param name="d"> /// The definition. /// </param> /// <param name="index"> /// The index. /// </param> /// <returns> /// A ComboBox. /// </returns> protected virtual FrameworkElement CreateComboBox(PropertyDefinition d, int index) { var c = new ComboBox { IsEditable = d.IsEditable, Focusable = false, Margin = new Thickness(0, 0, -1, -1) }; if (d.ItemsSource != null) { c.ItemsSource = d.ItemsSource; } else { if (d.ItemsSourceProperty != null) { var itemsSourceBinding = new Binding(d.ItemsSourceProperty); c.SetBinding(ItemsControl.ItemsSourceProperty, itemsSourceBinding); } } c.DropDownClosed += (s, e) => FocusParentItemsGrid(c); var binding = d.CreateBinding(index); binding.NotifyOnSourceUpdated = true; c.SetBinding(d.IsEditable ? ComboBox.TextProperty : Selector.SelectedValueProperty, binding); return c; }