I Have a method that add extra colums on a datagrid but I there is no data in it how can I add a DataSoure to the field. there are already a other datasource set on the grid ?
private void konkurrenter_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { var selected = combokonkurrenter.SelectedItem as Konkurent; if (selected != null) { var konkurrent = KonkurrenceDaekningDao.GetKonkurrenterById(selected.KonkurrentID); UnboundField imageField = new UnboundField();
imageField.DataContext = new {data = konkurrent}; konkurrent.ForEach(dao => { Console.WriteLine(dao.DækningsGrad); } ); imageField.Label = selected.KonkurrentNavn; imageField.Name = "Oplag"; dgKonkurrenter.FieldLayouts[0].Fields.Add(imageField); } }
Hello Thomas,
In my previous response I mentioned using converter that implements IMultiValueConverter with style for CellValuePresenter, however the binding was interfering after the converter had converted the value of the cell resulting in setting the whole collection as value of the cells ignoring the converter.
Because of this, I decided to use another approach with UnboundField. I am sending a small sample application. In its ViewModel there are 4 different data tables, one for the main data of the grid called peopleData and three other data tables used for the data of the UnboundFields. Similarly to your scenario, the UnboundFields are created after the user selects an item from the XamComboEditor.
Please test the sample on your side and let me know how it behaves or if you have any questions regarding it. If this is not an accurate demonstration of what you are trying to achieve, please feel free to modify it and send it back to me along with steps to reproduce. Alternatively, please feel free to provide your own sample.
Regards, Ivan Kitanov
AddNewFieldWithDSBasedOnComboEditor.zip
Hey Ivan
I think that I would like some more assistance
To prevent the newly added field from showing only the first ID value of the collection that the field is bound to, what I can suggest you is binding to the whole collection itself. After that, a style for the CellValuePresenter of the field should be added and applied when the field is added to the XamDataGrid, inside konkurrenter_SelectedItemChanged. The style should have a setter for the Value property its value should be provided by MultiBinding Converter. This converter would have 2 values one for the collection itself and one for the current row index. The style should be similar to the XAML below:
<Style x:Key="Style1" TargetType="{x:Type igWPF:CellValuePresenter}">
<Setter Property="Value">
<Setter.Value>
<MultiBinding Converter="{StaticResource converter}">
<Binding RelativeSource="{RelativeSource Self}" Path="Value"/>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type igWPF:DataRecordPresenter}}" Path="DataRecord.Index"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
The converter class should implement IMultiValueConverter. Inside the convert method, the bindings that were defined inside the style could be accessed like this:
var collection = values[0] as [The type of the collection KonkurrenceDaekningDao];
int index = (int)values[1];
After that, the return statement should be similar to this line:
return collection[index].ID;
Please let me know if you need further assistance.
Hi now there is data it the column but it looks wrong it puts the same value in all rows
it should have data like this
In order the values inside the field to appear they need to be bound. To do this instead of setting the DataContext of the field, a binding should be set to it, similar to this:
private void konkurrenter_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
var selected = combokonkurrenter.SelectedItem as Konkurent;
if (selected != null)
var konkurrent = KonkurrenceDaekningDao.GetKonkurrenterById(selected.KonkurrentID);
var imageField = new TextField();
imageField.AlternateBinding = new Binding()
Path = new PropertyPath(“ID”),
Mode = BindingMode.OneWay,
Source = konkurrent
};
imageField.Label = BindingMode.OneWay;
imageField.Name = "Oplag”;
imageField.Label = selected.KonkurrentNavn;
dgKonkurrenter.FieldLayouts[0].Fields.Add(imageField);
}
To learn more about adding an UnboundField and then binding it to a certain datasource, please visit this page. In it there is an example that shows how an UnboundField is added and bound in both XAML and programmatically.
Please let me know if you have any questions.