简体   繁体   English

Datagridcomboboxcolumn中的多项选择

[英]Multiple Selection in Datagridcomboboxcolumn

I need a DataGridComboBoxColumn which should allow multiple selection.我需要一个允许多项选择的 DataGridComboBoxColumn。 To do this, I created a DataGridComboBoxColumn populated with CheckBoxes.为此,我创建了一个填充有 CheckBox 的 DataGridComboBoxColumn。 I handle the Checkbox selection through CommandManager.PreviewExecuted event selecting the check boxes based on stored strings separated by ';'我通过 CommandManager.PreviewExecuted 事件处理复选框选择,根据存储的字符串选择复选框,以“;”分隔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.在 CellEditEnding 事件中,我根据复选框项目创建一个字符串并将其存储回表中。 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.用于从字符串检查 comboBox CheckBox 项目并将检查项目整理回字符串的代码由工作正常的 ComboBoxFill 方法处理。 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.但是,除非用户 select 和 combobox 用户无法知道选择了什么。 I have a previous DataGridTextColumn which provides feedback.我有一个以前提供反馈的 DataGridTextColumn。 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.我希望将 DataGridTextColumn 和 DataGridComboBoxColumn 放在一列中,或者使用更好的方法来使用多个 select DataGridComboBoxColumn 我的部分 XAML 看起来像这样。 Of interest is MaterialType Text column and MaterialType combo column.感兴趣的是 MaterialType Text 列和 MaterialType 组合列。 I need to have this under one column so it will be more user friendly.我需要将它放在一列下,这样它会更加用户友好。 执行程序

<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.任何帮助将不胜感激。

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.解决方案在 ComboBox 文本字段中。 By setting the value for this field, I am able to produce the result that I wanted - Displaying multi selected ComboBox.通过设置该字段的值,我能够产生我想要的结果 - 显示多选 ComboBox。 Here is the XAML code:这是 XAML 代码:

                <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.现在只需连接文本预览输入和鼠标事件以打开 Combobox。 That is easily achieved by triggering the BeginEdit method.这很容易通过触发 BeginEdit 方法来实现。 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 feature希望这可以帮助其他寻找此类功能的人材料类型列为选定元素,用 ; 分隔

带有选择清单的组合框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM