简体   繁体   中英

WPF Datagrid ComboBox options different based on another row value

I have a datagrid where the ItemsSource is set in code-behind for example:

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 x:Key="StatusList"  CollectionViewType="ListCollectionView"/>

And set when the window is loaded like so:

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.

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".

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 . I would recommend you to put your filtering logic in the view model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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