WPF MVVM Light Cannot trigger DataGrid AutogeneratingColumn event to dynamically manage columns

556 Views Asked by At

I want to dynamically manage the columns of a Datagrid which is buond to an EF element and should display only columns selected from a ListBox.

I use MVVM Light toolkit and when I try to use and fire the AutoGeneratingColumns event it doesn't work. It never triggers, even on the first generation of the DataGrid.

This is my viewmodel code :

 private void dgArticles_ColumnGeneration(DataGridAutoGeneratingColumnEventArgs args)
        {
            string headername = args.Column.Header.ToString();

            foreach (Champ c in LstSelected)
            {
                if (headername != c.Libelle)
                {
                    args.Cancel = true;
                }
                args.Column.Header = c.Libelle;
            }
        }

And this is my view xaml code for the DataGrid :

 <DataGrid Name="dgArticles"
              IsEnabled="True"
              Grid.Column="2"
              Grid.Row="1"
              AutoGenerateColumns="True"
              ItemsSource="{Binding LstArticles}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="AutoGeneratingColumn">
                <Command:EventToCommand Command="{Binding GenerateColumns}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>

On final purpose, I'd like to use a Button that would trigger this event to refresh the DataGrid when the user has modified the fields he selected in the ListBox.

Thanks for your help !

EDIT : Of course I declare my command in the constructor of my VM :

 public ExportViewModel()
        {
            GenerateColumns = new RelayCommand<DataGridAutoGeneratingColumnEventArgs>(dgArticles_ColumnGeneration);
            LstSelected = new ObservableCollection<Champ>();
            LstArticles = exp.ListerArticles();
        }
1

There are 1 best solutions below

0
On BEST ANSWER

I've solved the problem myself, it was a syntax error on "columnS" that should have been column in my xaml...

The event is triggered correctly.