2

I've got a DataGrid which contains a DataGridComboBoxColumn. What I want to do is to (dis)enable other columns based on the value of this combobox. For that, I handle the SelectionChanged event via an ICommand, but this command is never called. It works fine for comboboxes outside a DataGrid, but what is the trick inside a DataGrid ?

First remark, the value of Options is set only when the row is no more in edition mode.

Second remark, when enter key is pressed to end editing, the Options is not set even if it has been changed with the ComboBox

 <DataGrid Grid.Row="0" AutoGenerateColumns="False" 
              ItemsSource="{Binding Path=AcquisitionList, Mode=TwoWay}"
              SelectedItem="{Binding SelectedParameters}"
              Margin="0,20,0,0" Name="dataGridAcquisitions" 
              CanUserAddRows="True" CanUserDeleteRows="True"
              CanUserReorderColumns="False" SelectionUnit="FullRow">
            <DataGridComboBoxColumn Header="Choice1" Width="SizeToHeader"
                                    SelectedItemBinding="{Binding Options}"
                                    ItemsSource="{Binding Source={StaticResource OptionValues}}"
                                    EditingElementStyle="{StaticResource StandardComboBox}"
                                    ElementStyle="{StaticResource StandardComboBox}" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding EnableCustomParameters}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </DataGridComboBoxColumn>
            <DataGridComboBoxColumn Header="Choice2" Width="SizeToHeader"
                                    SelectedItemBinding="{Binding FilterType}"
                                    ItemsSource="{Binding Source={StaticResource FilterTypes}}"
                                    EditingElementStyle="{StaticResource StandardComboBox}"
                                    ElementStyle="{StaticResource StandardComboBox}" >
                <DataGridComboBoxColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="IsEnabled" Value="{Binding CustomParametersEnabled}"/>
                    </Style>
                </DataGridComboBoxColumn.CellStyle>
            </DataGridComboBoxColumn>
</DataGrid>

In the VM

  private RelayCommand enableCustomParameters;
  private Model.AcquisitionParameters selectedParameters;
  private ObservableCollection<Model.AcquisitionParameters> acquisitionList = new ObservableCollection<Model.AcquisitionParameters>();

  public ObservableCollection<Model.AcquisitionParameters> AcquisitionList
  {
     get { return acquisitionList; }
     set { acquisitionList = value; OnPropertyChanged("AcquisitionList"); }
  }

  public Model.AcquisitionParameters SelectedParameters
  {
     get { return selectedParameters; }
     set { selectedParameters = value; OnPropertyChanged("SelectedParameters"); }
  }

  public ICommand EnableCustomParameters
  {
     get
     {
        if(this.enableCustomParameters == null)
        {
           this.enableCustomParameters = new RelayCommand(
               param => this.ChangeCustomState());
        }
        return this.enableCustomParameters;
     }
  }

  public void ChangeCustomGainState()
  {
     SelectedParameters.CustomGainParametersEnabled = SelectedParameters.GainModeOptions == GainModeOptions.Custom;
  }

And the Model

public class AcquisitionParameters : INotifyPropertyChanged
{
  private Choice1 options;
  private Choice2 filterType;
  private bool customParametersEnabled;

  public Choice1 Options
  {
     get { return options; }
     set { options= value; OnPropertyChanged("Options"); }
  }

  public Choice2 FilterType
  {
     get { return filterType; }
     set { filterType= value; OnPropertyChanged("FilterType"); }
  }

  public bool CustomParametersEnabled
  {
     get { return customParametersEnabled; }
     set { customParametersEnabled = value; OnPropertyChanged("CustomParametersEnabled"); }
  }
}
GuillaumeA
  • 3,493
  • 4
  • 35
  • 69
  • I think this could help: http://stackoverflow.com/questions/19322271/wpf-datagrid-combobox-column-how-to-manage-event-of-selection-changed BR, Daniel – dba Mar 21 '17 at 12:49

2 Answers2

0

You should use appropriate binding:

{Binding Path=yourCommandName, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}

Read more about different bindings from here.

Community
  • 1
  • 1
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
  • 1
    Thanks for the reply, but it does not work either. I tried this solution before without success. I tried to bind to DataGrid or UserControl ancestor, but it failed. I have the same issue with the ItemsSource of the Choice1 ComboBox. For that, the workaround was to create a static resource as the choice list does not change and is property of the control. – GuillaumeA Feb 17 '15 at 13:06
0

It is not really a solution, but a workaround. As I did not find a way to fire the event when the combobox item is changed, I used the property Options for that.

First I must decorate the SelectedItemBinding with the attribute "UpdateSourceTrigger=PropertyChanged" which specify the timing of the binding source to update. From doc, PropertyChanged "Updates the binding source immediately whenever the binding target property changes." That's exactly the behavior I was looking for.

Then, the Model was modify as follow:

public class AcquisitionParameters : INotifyPropertyChanged
{
  private Choice1 options;
  private Choice2 filterType;
  private bool customParametersEnabled;

  public Choice1 Options
  {
     get { return options; }
     set 
     { 
        options= value; 
        CustomParametersEnabled = options == Choice1.Options1;
        OnPropertyChanged("Options"); 
        OnPropertyChanged("CustomParametersEnabled "); 
     }
  }

  public Choice2 FilterType
  {
     get { return filterType; }
     set { filterType= value; OnPropertyChanged("FilterType"); }
  }

  public bool CustomParametersEnabled
  {
     get { return customParametersEnabled; }
     set { customParametersEnabled = value; OnPropertyChanged("CustomParametersEnabled"); }
  }
}

Then, when the first ComboBox for the parameter Options has its selected item changed, the property Options is immediately set, and there I update also the CustomParametersEnabled too. That's not the way I wanted to do at first attempt, but it works.

GuillaumeA
  • 3,493
  • 4
  • 35
  • 69