I am building a XamMultiSelectionComboEditor programatically for use inside a XamDataCards control (part of our applications dynamic property editor infrastructure). I have been able to make the editor show up in the data card, and if I enable auto generation of columns, it shows my data, but I am trying to figure out how I can add a TextComboColumn programatically.
Any help would be appreciated, what I have so far is below (note auto egneration is false in this snippet).
Thanks
Jonathan
public static Field GetComboEditor(string name, string label, object values, string displayMemberPath, IValueConverter converter, ValueConstraint constraint = null, string tooltip = null) { var editor = new FrameworkElementFactory(typeof(XamMultiColumnComboEditor)); editor.SetValue(XamMultiColumnComboEditor.AutoGenerateColumnsProperty, false); editor.SetValue(XamMultiColumnComboEditor.AllowMultipleSelectionProperty, false); editor.SetValue(XamMultiColumnComboEditor.ItemsSourceProperty, values); editor.SetValue(XamMultiColumnComboEditor.DisplayMemberPathProperty, displayMemberPath); editor.SetValue(XamMultiColumnComboEditor.SelectedItemsResetButtonVisibilityProperty, Visibility.Collapsed);
// BUILD COLUMNS AND ATTACH TO THE editor HERE SOME HOW?
var ctrlTemplate = new ControlTemplate(typeof(CellValuePresenter)) { VisualTree = editor }; var cvpStyle = new Style(typeof(CellValuePresenter)); cvpStyle.Setters.Add(new Setter(Control.TemplateProperty, ctrlTemplate)); return new Field { Name = name, Label = label, IsExpandable = false, ToolTip = tooltip, DataType = typeof(object), Settings = { CellValuePresenterStyle = cvpStyle, } }; }
Hello Jonathan,
I am glad to know that you have achieved the desired functionality in regards to populating the Columns collection.
The AlternateBinding you have used does not have a Source property set and it is expected not to work it his case.This type of binding is mostly used for describing a path of properties for the DataItem when it is of a complex type, which means your original data should contain such complex type property or you should provide another specific Source.
If the field you are adding to the XamDataGrid is not connected to any of the DataItem's properties, using the UnboundBinding should work for achieving the functionality you are looking for.
If you have any questions, please let me know.
Thanks. I got it working as follows...
var editor = new FrameworkElementFactory(typeof(XamMultiColumnComboEditor)); editor.SetValue(XamMultiColumnComboEditor.AutoGenerateColumnsProperty, false); editor.SetValue(XamMultiColumnComboEditor.AllowMultipleSelectionProperty, false); editor.SetValue(XamMultiColumnComboEditor.ItemsSourceProperty, values); editor.SetValue(XamMultiColumnComboEditor.DisplayMemberPathProperty, displayMemberPath); editor.SetValue(XamMultiColumnComboEditor.SelectedItemsResetButtonVisibilityProperty, Visibility.Collapsed); editor.AddHandler(FrameworkElement.LoadedEvent, new RoutedEventHandler((s, a) => { (s as XamMultiColumnComboEditor).Columns.Add(new TextComboColumn {Key = displayMemberPath}); }));
Now, the last issue is that the binding doesn't seem to work...
var editor = new FrameworkElementFactory(typeof(XamMultiColumnComboEditor)); editor.SetValue(XamMultiColumnComboEditor.AutoGenerateColumnsProperty, false); editor.SetValue(XamMultiColumnComboEditor.AllowMultipleSelectionProperty, false); editor.SetValue(XamMultiColumnComboEditor.ItemsSourceProperty, values); editor.SetValue(XamMultiColumnComboEditor.DisplayMemberPathProperty, displayMemberPath); editor.SetValue(XamMultiColumnComboEditor.SelectedItemsResetButtonVisibilityProperty, Visibility.Collapsed); editor.AddHandler(FrameworkElement.LoadedEvent, new RoutedEventHandler((s, a) => { (s as XamMultiColumnComboEditor).Columns.Add(new TextComboColumn {Key = displayMemberPath}); })); var cvpStyle = new Style(typeof(CellValuePresenter)); cvpStyle.Setters.Add(new Setter(Control.TemplateProperty, new ControlTemplate(typeof(CellValuePresenter)) {VisualTree = editor})); return new Field { Name = name, Label = label, IsExpandable = false, ToolTip = tooltip, DataType = typeof(object), BindingType = BindingType.UseAlternateBinding, AlternateBinding = new Binding(name) { Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }, Converter = converter, Settings = { CellValuePresenterStyle = cvpStyle, } };
Any ideas on hte binding issue please?
Thank you for the feedback.
Using the FrameworkElementFactory and the ControlTemplate for populating the Columns collection will not be enough, since the Columns property is read-only and the template does not provide enough information. In order to access the Columns, we will have to reference the editor itself.In order to do this within the same method, I can suggest you create an additional EventSetter for the Loaded event of the CellValuePresenter and add it to the Setters of the already existing Style you have created.
This way you will be able to handle the Loaded event and by getting the XamMultiColumnComboEditor from the visual tree of the presenter, you can access the Columns collection.
...// BUILD COLUMNS AND ATTACH TO THE editor HERE SOME HOW?var ctrlTemplate = new ControlTemplate(typeof(CellValuePresenter)){ VisualTree = editor}; var loadedCellValuePresenterHandler = new RoutedEventHandler((se, ev) =>{ var cvp = (se as CellValuePresenter); var multiCombo = Utilities.GetDescendantFromType(cvp, typeof(XamMultiColumnComboEditor), false) as XamMultiColumnComboEditor; if (multiCombo == null) return; multiCombo.Columns.Add(new TextComboColumn() { Key = "Name" });}); var cvpStyle = new Style(typeof(CellValuePresenter));cvpStyle.Setters.Add(new EventSetter(Control.LoadedEvent, loadedCellValuePresenterHandler));cvpStyle.Setters.Add(new Setter(Control.TemplateProperty, ctrlTemplate));...
var loadedCellValuePresenterHandler = new RoutedEventHandler((se, ev) =>{ var cvp = (se as CellValuePresenter); var multiCombo = Utilities.GetDescendantFromType(cvp, typeof(XamMultiColumnComboEditor), false) as XamMultiColumnComboEditor; if (multiCombo == null) return;
multiCombo.Columns.Add(new TextComboColumn() { Key = "Name" });}); var cvpStyle = new Style(typeof(CellValuePresenter));cvpStyle.Setters.Add(new EventSetter(Control.LoadedEvent, loadedCellValuePresenterHandler));cvpStyle.Setters.Add(new Setter(Control.TemplateProperty, ctrlTemplate));...
I have modified and attached the sample from my previous reply with the approach from above.
Thank you. This unfortunately doesn't really answer my question. I really need to know how to set the columns when all I have is...
var editor = new FrameworkElementFactory(typeof(XamMultiColumnComboEditor)); editor.SetValue(XamMultiColumnComboEditor.AutoGenerateColumnsProperty, false); editor.SetValue(XamMultiColumnComboEditor.AllowMultipleSelectionProperty, false); editor.SetValue(XamMultiColumnComboEditor.ItemsSourceProperty, values); editor.SetValue(XamMultiColumnComboEditor.DisplayMemberPathProperty, displayMemberPath); editor.SetValue(XamMultiColumnComboEditor.SelectedItemsResetButtonVisibilityProperty, Visibility.Collapsed); var ctrlTemplate = new ControlTemplate(typeof(CellValuePresenter)) { VisualTree = editor };
Maybe I can add an eventtrigger to the controltemplate?
The resulting object is a Infragistics.Windows.DataPresenter.Field
Cheers
Hello robsonj,
Thank you for the code-snippet you have provided.
In order to manually specify the visible columns of the XamMultiColumnComboEditor when it is defined as a template for the CellValuePresenterStyle of a specific field in the XamDataCards, you can refer to the Columns collection of the editor and add new TextBoxColumn instances by specifying their respective Key.
XAML:
<igDP:XamDataCards.Resources> <Style TargetType="ig:XamMultiColumnComboEditor"> <EventSetter Event="Loaded" Handler="MultiCombo_Loaded" /> </Style></igDP:XamDataCards.Resources>
Code-behind:
private void MultiCombo_Loaded(object sender, RoutedEventArgs e){ // Add a columns to the XamMultiColumnComboEditor when it has loaded // (NOTE: Additional checks will be required to identify the ItemsSource of the Combo // and choose the appropriate column keys.) // (sender as XamMultiColumnComboEditor).Columns.Add(new TextComboColumn() { Key = "Name" });}
I have attached a sample that uses the approach from above.