1

I need a DataGridComboBoxColumn which should allow multiple selection. To do this, I created a DataGridComboBoxColumn populated with CheckBoxes. I handle the Checkbox selection through CommandManager.PreviewExecuted event selecting the check boxes based on stored strings separated by ';' My code:

            if (((DataGrid)sender).CurrentCell.Column.DisplayIndex == 6)
            {
                DataGridRow row = (DataGridRow)SupplierProductsGrid.ItemContainerGenerator.ContainerFromIndex(SupplierProductsGrid.SelectedIndex);
                DataGridCell RowColumn = SupplierProductsGrid.Columns[6].GetCellContent(row).Parent as DataGridCell;
                ComboBox cb = RowColumn.Content as ComboBox;
                if (cb != null)
                {
                    Debug.WriteLine("Entered Combobox editing");
                    ComboBoxFill(cb, true);
                }
            }

and in CellEditEnding event I create a string based on the checked box items and store that back to table. My code for that is:

        DataGridRow row = (DataGridRow)SupplierProductsGrid.ItemContainerGenerator.ContainerFromIndex(SupplierProductsGrid.SelectedIndex);
        DataGridCell RowColumn = SupplierProductsGrid.Columns[6].GetCellContent(row).Parent as DataGridCell;
        ComboBox cb = RowColumn.Content as ComboBox;
        if (cb != null)
        {
            //Debug.WriteLine("Entered Combobox Consolidation");
            ComboBoxFill(cb, false);
            return;
        }

The code for checking the comboBox CheckBox items from string and collating the checked items back to string is handled by ComboBoxFill method which works fine. No issue there. I am able to save to the table and retrieve the values. However unless the user select the combobox there is no way user knows what has been selected. I have a previous DataGridTextColumn which provides feedback. I would like to have both the DataGridTextColumn and DataGridComboBoxColumn in one column or a better approach to use multiple select DataGridComboBoxColumn My Partial XAML looks like this. Of interest is MaterialType Text column and MaterialType combo column. I need to have this under one column so it will be more user friendly.Executed Program

<DataGridTextColumn Binding="{Binding SupplierLeadTime}" Header="Lead Time" Width="1*" />
<DataGridComboBoxColumn x:Name="StorageUnitCombo"  Header="Package Unit"  SelectedValuePath="StorageUnitId" Width="2.5*" 
     DisplayMemberPath="Description" SelectedItemBinding="{Binding BaseStorageUnitNavigation}" />
<DataGridTextColumn x:Name="Materialtype" Header="Material Type" Width="140" Binding="{Binding MaterialType}" Visibility="Visible" ElementStyle="{StaticResource WT}" IsReadOnly="True"/>
<DataGridComboBoxColumn x:Name="MTCombo" Header="Material Type Combo" Width="140" ItemsSource="{Binding Source={StaticResource MaterialTypeViewSource}}" />

Any Help will be greatly appreciated.

1 Answer 1

1

After more Google Search and more Browsing of codes, most solutions were not to my requirement nor were that applicable to my solution. However I manage to get the gist of many codes and put together a simple solution that works like a charm. The solution is in the ComboBox Text field. By setting the value for this field, I am able to produce the result that I wanted - Displaying multi selected ComboBox. Here is the XAML code:

                <DataGridComboBoxColumn x:Name="MTCombo" Header="Material Type Combo" Width="2.5*" 
                                        ItemsSource="{Binding Source={StaticResource MaterialTypeViewSource}}" >
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property ="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <TextBlock Text="{Binding MaterialType}" Style="{StaticResource WTR}" 
                                             PreviewTextInput="TextBlock_PreviewTextInput" MouseDown="TextBlock_MouseDown"  />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="IsDropDownOpen" Value="True" />
                            <Setter Property="Text" Value="Select Material Types"/>
                            <Setter Property="IsReadOnly" Value="True"/>
                            <Setter Property="IsEditable" Value="True"/>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>

Now Only thing was to connect the Text Preview Input & Mouse event to open up the Combobox. That is easily achieved by triggering the BeginEdit method. Here is the code for that:

private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
    SupplierProductsGrid.BeginEdit();
}

The images speaks for themselves. Check the figure for Material Type combo, which displays all the selected elements separated by ;. Hope this helps others looking for such a featureMaterial Type Listed as selected elements separated by ;

Combobox with checklist for selection

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.