1

I would like to have a function to apply selected item to all combo box in the Data Grid. Currently what I have :

  1. Combo box to select which item to be applied to all combo box in the data grid. This has been placed on the header section
  2. Button to execute "Apply to all" command
  3. Combo box in each row of data grid.

The combo box is bind to the ObservableCollection of a Service class :

 public ObservableCollection<Services> ServiceList
        {
          get
          {
            return serviceList;
          }
          set
          {
            this.serviceList = value;
            RaisePropertyChanged("ServiceList");
          }
        }

This is how I apply the command to execute said function in the XAML :

<StackPanel Name="stackPanel2"
                    Grid.Row="1"
                    Grid.Column="1"
                    Width="335"
                    Height="26"
                    Margin="3,8,0,0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Orientation="Horizontal">
            <ComboBox Name="comboBox1"
                      Width="120"
                      Height="23"
                      DisplayMemberPath="DESCRIPTION"
                      ItemsSource="{Binding ServiceList}"
                      SelectedItem="{Binding SelectedServiceAll,Mode=TwoWay}"
                      />
            <Button Content="Apply to all"
                    Command="{Binding CommandApplyAll}"
                    CommandParameter="AllService"
                    >
            </Button>

</StackPanel>

and this is the data grid row combo box binding :

<DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Name="comboBoxService"
                                      Width="120"
                                      Height="23"
                                      DisplayMemberPath="DESCRIPTION"
                                      ItemsSource="{Binding Path=ServiceList}"
                                      SelectedValue="{Binding Path=SERVICE,
                                                              UpdateSourceTrigger=PropertyChanged}"                                       
                                      />

                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

Should I update the data source (ObservableCollection ServiceList) for Combo box in the data grid using the one has been selected in the header section but not sure how to achieve that.

Note : This application applying MVVM

Thanks a lot

anevil
  • 837
  • 3
  • 11
  • 24

1 Answers1

0

If I have not misunderstood your question you can do it like set the SERVICE(that is binded to SelectedValue) in ApplyCommand Action

    private void OnApplyCommand()
    {
        foreach (var item in ServiceGridDataList)
        {
            item.SERVICE = SelectedServiceAll.SERVICE;
        }
        SelectedGridService = selectedHeaderService;
        //Other apply command Action;
    }

Here ServiceGridDataList is the Collection that is binded to the ItemsSource of your Grid.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • Hi..It works by applying this line of code : ServiceGridDataList= ServiceGridDataList.Select(c => { c.SERVICE = SelectedServiceAll; return c; }).ToObservableCollection();..get from this http://stackoverflow.com/questions/398871/update-all-objects-in-a-collection-using-linq... – anevil Mar 16 '15 at 12:02