I'm using WPF XamDataGrid 23.2.157 in a MVVM project.
I want to use a XamDataGrid bound to a DataTable. The DataTable elements are just strings. The tables are dynamic (not known at compile time), and columns can be added/removed.
I want to be able to add columns, and then for each row in each column users select from a set of values (coming from the view model) in a combo box. Every cell should be bound to the same list of options.
I also want to be able to delete columns, preferrably with a delete 'x' in the column header.
I have started by handling FieldLayoutInitialized, but I'm wondering if there is a simple way to do this without handling events?
Hi Walter,
Thank you for posting to Infragistics Community!
I am attaching a sample app demonstrating a simple XamDataGrid with ComboBoxFields, whose ItemsSource is bound to a List from the ViewModel. This binding leverages the custom FieldBinding markup extension. More about this can be read in this page of our documentation.
Regarding the possibility to template a delete button in the fields’ labels, sure, this is also possible. A custom style targeting the LabelPresenter element has to be created and its ContentTemplate can be set to whatever content required, for example:
<Style TargetType="{x:Type igWPF:LabelPresenter}"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding}" Width="60"/> <Button Width="20" Click="Button_Click">X</Button> </StackPanel> </DataTemplate> </Setter.Value> </Setter> </Style>
In order to delete the field from the button’s handler, one has to retrieve it first, for example:
private void Button_Click(object sender, RoutedEventArgs e) { LabelPresenter labelPresenter = (LabelPresenter)Infragistics.Windows.Utilities.GetAncestorFromType((Button)sender, typeof(LabelPresenter), false); if(labelPresenter == null) { return; } Field field = labelPresenter.Field; }
However, since deleting is an irreversible action, I can also suggest considering simply hiding the field by setting its Visibility property to ‘Collapsed’:
// or hide it and allow it to be turned back form the Field Chooser field.Visibility = Visibility.Collapsed;
In this way, if the Field Chooser is also configured to be displayed, users could bring back any hidden fields. Of course, adopt this only, if the scenario allows it.
Now, in case hanlding the Button's Click event is not desried as an approac in your MVVM scenario, you could consider implementing it as a Behavior, for instance. There might be other approaches, depending on your app's configuration, however, this would be out of the scope of Infragistics components.
In conclusion, please, check out the referenced resources and let me know if they help.
Best regards,Bozhidara PachilovaAssociate Software Developer
6545.XDGDataTable.zip
Bozhidara,
Thank you for that sample. I probably didn't communicate this well, but my largest constraint is that I don't know the columns in advance, and they can be added and removed by the user. In your sample you are creating two predefined fields for Name and Country, but we don't have the ability to predefine them in code.
Thank you for clarifying.
While I understand the difference in this case, whether the fields are dynamically generated would be separate from the requirement about templating the label and showing/hiding/removing columns or creating ComboBoxFields, as far as I understand your scenario so far.
Anyways, I believe you wil find the “Define a Field Layout” documentation topic quite helpful in this case. There you can find snippets of programmatically defining fields for the XamDataGrid.
Doing this based on an unknown object, though, is a matter of coding it separately, for instance, a sample approach would be to iterate the DataTable’s columns and create fields according to their type and set with the column's name. Of course, the implementation for this would be subject to modifications on application level depending on the specific requirements and coding conventions.The fields can be ComboBoxFields as well and their ItemsSource can again be set to an IEnumerable of the local data context.
I have modified the previous sample to demonstrate a possible approach for this. Please, check it out and let me know if it accurately demonstrates what you’re trying to achieve. Alternatively, please, feel free to provide further details or modify the sample and send it back to me to communicate your ideas.
Best regards,Bozhidara Pachilova
4863.XDGDataTable2.zip
Thanks!
As a followup, it's working well so far. One behavior I'd like to add is to have the combo box commit when an item is selected. Currently I have to move to a different cell for it to be comitted and the new row updates, etc.
Is there and event or behavior I can handle to do that?