Question

I work with wpf & MVVM. I have a page with datagrid which is bound to a datatable from viewmodel . In datagrid auto generate column property is true . But I need some coulmns to be combo box . So i use AutoGeneratingColumn event of datagrid to achieve that. In code behind event method looks like this.

   private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
       if (e.PropertyName == "Gender")
        {
            var cb = new DataGridComboBoxColumn();
            cb.ItemsSource = (DataContext as EmpDetailsWindowViewModel).GenderDataTable.DefaultView;
            cb.DisplayMemberPath = "Name";
            cb.SelectedValuePath = "Code";
            cb.SelectedValueBinding = new Binding("Gender");
            e.Column = cb;
       }
    }

Now as per new requirement I have to use infragistics xamDatagrid or Xamgrid for filtering values and for some other features which infragistics grid provide . But I am not finding AutoGeneratingColumn event in both of the infragistics grids . How can achieve this functionality in infragistics grids?

Was it helpful?

Solution 2

In that case subscribe to the FieldLayoutInitialized event and write appropriate code logic.

I just wrote this code:

private void XamDataGrid_FieldLayoutInitialized_1(object sender,   Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e)
    {
        var comboGenderField = e.FieldLayout.Fields["Gender"];
        if (comboGenderField != null)
        {
            var xamComboEditorStyle = new Style(typeof(XamComboEditor));
            var itemsProviderSetter = new Setter(XamComboEditor.ItemsProviderProperty,
                                                 this.FindResource("cmbGenderProvider"));
            xamComboEditorStyle.Setters.Add(itemsProviderSetter);

            comboGenderField.Settings.EditorStyle = xamComboEditorStyle;
            comboGenderField.Settings.EditAsType = typeof (int);
        }
    }

Refer this for more info: http://help.infragistics.com/Help/NetAdvantage/WPF/2012.2/CLR4.0/html/xamComboEditor_Setting_the_xamComboEditor_as_an_Editor_of_a_Field_Programmatically.html

OTHER TIPS

It's pretty simple, see this link for details

http://help.infragistics.com/NetAdvantage/WPF/2011.1/CLR4.0/?page=xamComboEditor_Using_xamComboEditor_to_Edit_a_Field_in_xamDataGrid.html

In a nutshell:

First add a ComboBoxItemsProvider to your resources page

<Window.Resources>
    <igWPF:ComboBoxItemsProvider x:Key="cmbGenderProvider">
        <igWPF:ComboBoxDataItem DisplayText="Male" Value="0"/>
        <igWPF:ComboBoxDataItem DisplayText="Female" Value="1"/>
    </igWPF:ComboBoxItemsProvider>
</Window.Resources>

And Edit the Field Layout of the DataGrid in the XAML and set the ItemsProvider property of the XamComboEditor to the above resource:

<Grid>
    <igDP:XamDataGrid HorizontalAlignment="Left" DataSource="{Binding DataTable}">
        <igDP:XamDataGrid.FieldLayoutSettings>
            <igDP:FieldLayoutSettings AutoGenerateFields="False" />
        </igDP:XamDataGrid.FieldLayoutSettings>
        <igDP:XamDataGrid.FieldLayouts>
        <igDP:FieldLayout>
            <igDP:FieldLayout.Fields>
                <igDP:Field Name="Name" Label="Name"/>
                <igDP:UnboundField Name="Gender" Label="Gender">
                    <igDP:Field.Settings>
                        <igDP:FieldSettings EditAsType="{x:Type sys:Int32}" EditorType="{x:Type igWPF:XamComboEditor}">
                            <igDP:FieldSettings.EditorStyle>
                                <Style TargetType="{x:Type igWPF:XamComboEditor}">
                                    <Setter Property="ItemsProvider" Value="{StaticResource cmbGenderProvider}"/>
                                </Style>
                            </igDP:FieldSettings.EditorStyle>
                        </igDP:FieldSettings>
                    </igDP:Field.Settings>
                </igDP:UnboundField>
            </igDP:FieldLayout.Fields>
        </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>
    </igDP:XamDataGrid>
</Grid>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top