Hello
I need to create a XamDataGrid with 2 columns. 1st column should be Text and 2nd is ComboBox. The data for the 1st Text column I have in Collection in codebehind but the 2nd Combobox I need to fill with data from another source (WCF service) based on the value in 1st Column.
Also the 1st Text column is binded to the CollectionData and the selected value in 2nd column of Combobox should be binded to another source.
All this I need to do in codebehind, but I would really appreciate also any help in XAML as well.
I was able to do something like this:
......
ControlTemplate ctrlComboTemplate = new ControlTemplate(typeof(CellValuePresenter));
FrameworkElementFactory frmComboElement = new FrameworkElementFactory(typeof(ComboBox), "cmbAddStCode");
ctrlComboTemplate.VisualTree = frmComboElement;
comboStyle.Setters.Add(setter);
bind.Source = this._comboData;
frmComboElement.SetBinding(ComboBox.SelectedValueProperty, bind);
field.Settings.CellValuePresenterStyle = comboStyle;
.....
ComboBox cm = sender as ComboBox;
cm.ItemsSource = comboBoxItems;}
but in this Test handler I dont know in which Row Im currently in, so all the Comboboxes are filled with the same values. Also the bindings are made to the same source while the binding was made in Template.
Please help
thank you very much
I found a very simple solution for this problem (maybe I have missed something before, but after a little investigation). I have changed the style to XamTextEditor and set the bindings to Combobox TextProperty directly to Template as a TemplatedParent. So now I don`t need to do aditional bindings. See the code:
.... Style comboStyle = new Style(typeof(XamTextEditor));ControlTemplate ctrlComboTemplate = new ControlTemplate(typeof(XamTextEditor));FrameworkElementFactory frmComboElement = new FrameworkElementFactory(typeof(ComboBox), "cmbAddStCode");Binding bind = new Binding("Text");bind.RelativeSource = RelativeSource.TemplatedParent;frmComboElement.SetBinding(ComboBox.TextProperty, bind);ctrlComboTemplate.VisualTree = frmComboElement;Setter setter = new Setter(XamTextEditor.EditTemplateProperty, ctrlComboTemplate);comboStyle.Setters.Add(setter);frmComboElement.AddHandler(ComboBox.LoadedEvent, new RoutedEventHandler(Test));frmComboElement.SetValue(ComboBox.IsEditableProperty, true);field.Settings.EditorStyle = comboStyle;....
void Test(object sender, RoutedEventArgs e){ComboBox cm = sender as ComboBox;cm.ItemsSource = Countries;DataRecord dr = cm.DataContext as DataRecord;Data data = dr.DataItem as Data;if (data != null){ // Do something here }}
Yes, Im already catching the RecordAdded event from XamDataGrid but there I don`t know how to add binding to the Combobox to the newly created Data as I am doing in my Test method. I can reach the Data in RecordAdded event but how can I reach the instance of Combobox?Or am I missing something?
In the other hand I think this is not a good approach anyway to do the same bindings in two places. I was trying to add the bindings in a ControlTemplate but then it binds every row to the same object so when I have changed the Combobox item in one row it has changed in every other rows.
void Test(object sender, RoutedEventArgs e){ ComboBox cm = sender as ComboBox; // HERE I HAVE THE COMBOBOX INSTANCE DataRecord dr = cm.DataContext as DataRecord; Data data = dr.DataItem as Data; Binding bind = new Binding("Country"); bind.Source = dr.DataItem; bind.Mode = BindingMode.TwoWay; cm.SetBinding(ComboBox.SelectedValueProperty, bind); // HERE I AM BINDING THE Data TO SELECTEDVALUE of ComboBox}
void myDataGrid_RecordAdded(object sender, Infragistics.Windows.DataPresenter.Events.RecordAddedEventArgs e){ // How can I bind the new created Data with the Combobox?}
Thank you again for your help.
You can listen to the RecordAdded event (this event is raised during the 1st edit of a cell in the add record). Then to get to the CellValuePresenter for a specific cell you can call its static FromRecordandField method. Note: you may have to call the XamDataGrid's UpdateLayout method first to ensure that the element tree was hydrated.
If you want to initialize things before the 1st edit then listen to the InitializeTemplateAddRecord event instead.
Thank you very much for your replies. OK now I am able to fill the comboboxes with different values and with different bindings as well.(see code below)
When the new record is added the AddNew method is called and new Data object is added to my IBindingList. The Test method is called after but again only to empty NEW RECORD row so the new added records are not Binded with Comboboxes.
Do you have any idea how can I bind Combobox with newly added Data?
....
DataRecord dr = cm.DataContext as DataRecord; // Thank you joedour!
Data data = dr.DataItem as Data;
if (data != null) // checking to avoid binding to NEW RECORD row
bind.Source = dr.DataItem;
bind.Mode = BindingMode.TwoWay;
}
thanks in advance for any help
Hi,
Had the same situation where I was using 2 comboBoxes and one combox was linked to another. I did it through comboBox handlers. You must have realized that when we try edit the field containing the comboBox, it is then the comboBox_loaded function is called. SO I used to grab that particular record, fill in the combobox with the desired value and then get that thing displayed in my combobox. So way out may be digging deep into the EditModeStarting, and ComboBox Load functions.
Hope u find a better way out.
Thanks
Nasir