简体   繁体   English

WPF Datagrid ComboBox 选项因另一行值而异

[英]WPF Datagrid ComboBox options different based on another row value

I have a datagrid where the ItemsSource is set in code-behind for example:我有一个数据网格,其中 ItemsSource 在代码隐藏中设置,例如:

var grid = grdEmploy as DataGrid;
grid.ItemsSource = employments; // list of objects

In this grid, I have several drop downs being used when editing the row.在这个网格中,我在编辑行时使用了几个下拉菜单。 The options are currently held in a local CollectionViewSource, for example:这些选项当前保存在本地 CollectionViewSource 中,例如:

<CollectionViewSource x:Key="StatusList"  CollectionViewType="ListCollectionView"/>

And set when the window is loaded like so:并设置 window 的加载时间,如下所示:

var statusList= Functions.GetStatuses(); // returns a List<> 
CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)(FindResource("StatusList"));
itemCollectionViewSource.Source = statusList;

Then the binding of the column for the grid would look like so:然后网格列的绑定将如下所示:

<DataGridTemplateColumn Header="Employment Status" HeaderStyle="{StaticResource WrappedColumnHeaderStyle}">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock>
                                            <TextBlock.Text>
                                                <MultiBinding>
                                                    <MultiBinding.Converter>
                                                        <local:AimTypeConverter />
                                                    </MultiBinding.Converter>
                                                    <Binding Path="EmpStat" />
                                                    <Binding Path="SourceCollection" Source="{StaticResource StatusList}" />
                                                </MultiBinding>
                                            </TextBlock.Text>
                                        </TextBlock>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <ComboBox SelectedValue="{Binding EmpStat}" SelectedValuePath="Value" DisplayMemberPath="Text" ItemsSource="{Binding Source={StaticResource StatusList}}"></ComboBox>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>

This all works great however I have hit a snag whereby one of the columns needs to show different options based on another column.这一切都很好,但是我遇到了一个障碍,其中一个列需要根据另一列显示不同的选项。 For example if Column A is "1" show Options 2,3, if "2", show Options 3,4 etc.例如,如果 A 列为“1”,则显示选项 2,3,如果为“2”,则显示选项 3,4 等。

My thoughts were to load all options into the local list and somehow filter them but I'm not sure how best to do this, any help on this would be appreciated.我的想法是将所有选项加载到本地列表中并以某种方式过滤它们,但我不确定如何最好地做到这一点,对此的任何帮助将不胜感激。

The way to solve this using the MVVM pattern would be to define a collection property in the Employee class, or whatever you call it, and then return an already filtered collection from this property based on the value of the property bound to "Column A".使用 MVVM 模式解决此问题的方法是在Employee class 中定义一个集合属性,或者您所称的任何名称,然后根据绑定到“列 A”的属性的值从该属性返回一个已过滤的集合.

I am afraid it doesn't make much sense to define a single source collection in the code-behind if you want to bind to several source collections, filtered or not, in the DataGrid .如果您想绑定到DataGrid中的多个源 collections(过滤与否),恐怕在代码隐藏中定义单个源集合没有多大意义。 I would recommend you to put your filtering logic in the view model.我建议您将过滤逻辑放在 model 视图中。

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

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