Search code examples
c#wpfselectiondatagridcomboboxcolumnitemsource

Disable selection of specific item in DatagridComboBox


Background: I have a Datagrid with some Measurements and this Measurements we can Approve and Block. Now we have for this a new Type, like "Cancelled". But this Type is only needed by Server and for displaying it to Customer. But the Customer should not be able to select this "Cancelled" but the other 2 Types he should have to select.

The List get all different elements from Database (3 entries). Firstly i tried to remove the Cancelled Item from the ApprovementCollection, but then it displayed a empty field instead of "Cancelled".

Question: Is it possible, to disable only one of this 3 Items in the Bounded List of the Itemsource Property from the DataGridComboBoxColumn?

Disabled or Not Displayed in the Selection Menu is that what i have to do. (Only "Freigabe" and "GESPERRT")

View List in action

View:

<DataGridComboBoxColumn ClipboardContentBinding="{x:Null}"
                                        DisplayMemberPath="ApprovementText"
                                        Header="{x:Static trans:Translations.ClearenceHeader}"
                                        ItemsSource="{Binding Source={StaticResource PossibleComponentMeasurementApprovements}}"

                                        SelectedItemBinding="{Binding Approvement,
                                                                      UpdateSourceTrigger=PropertyChanged}" />

Viewmodel:

private IEnumerable<ComponentMeasurementApprovement> possibleComponentMeasurementApprovements;
    public IEnumerable<ComponentMeasurementApprovement> PossibleComponentMeasurementApprovements {
        get { return possibleComponentMeasurementApprovements; }
        set {
            possibleComponentMeasurementApprovements = value;
            OnPropertyChanged();
        }
    }

Thanks for your Help


Solution

  • This is possible writing a DataGridTemplateColumn for your cell instead of using the DataGridComboBoxColumn. Just add a property Enabled to your ComponentMeasurementApprovement class. This property indicates if the user is allowed to select the approvement.

    Now create a new DataGridTemplateColumn containing a ComboBox as template. It is possible to bind IsEnabled of every ComboBox item to a proeprty by styling them via ItemContainerStyle.

    Here is the code:

    <DataGridTemplateColumn Header="CustomCell">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ComboBox ItemsSource="{Binding Source={x:Static local:ViewModel.PossibleComponentMeasurementApprovements}}"
                            DisplayMemberPath="ApprovementText"
                            SelectedItem="{Binding Approvement}">
                    <ComboBox.ItemContainerStyle>
                        <Style TargetType="{x:Type ComboBoxItem}">
                            <Setter Property="IsEnabled" Value="{Binding Enabled}"/>
                        </Style>
                    </ComboBox.ItemContainerStyle>
                </ComboBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    

    Here is the result:

    enter image description here

    Since the second item is disabled, it's not possible to select it but an already selected item keeps selected.