WPF : How to disable some items in a Datagrid combobox column for some specific cells?

Tarunraj 21 Reputation points
2021-06-24T05:27:03.54+00:00

I have a datagrid combobox column and the dropdown has a few items.
On some specific cells in the same column , I need some items in dropdown to be disabled.
Thanks in advance ! :)

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,678 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2021-06-24T08:22:24.83+00:00

    I made a sample for your question base on my understanding, here is the code.
    XAML code:

     <Window.Resources>  
            <Style TargetType="{x:Type ComboBoxItem}">  
                <Setter Property="Template">  
                    <Setter.Value>  
                        <ControlTemplate TargetType="{x:Type ComboBoxItem}">  
                            <Border x:Name="Border" SnapsToDevicePixels="true" IsHitTestVisible="{Binding IsBool}">  
                                <TextBlock Text="{Binding ItemName}" IsHitTestVisible="{Binding IsBool}" >  
                                    <TextBlock.Style>  
                                        <Style TargetType="TextBlock">  
                                            <Style.Triggers>  
                                                <Trigger Property="IsHitTestVisible" Value="false">  
                                                    <Setter Property="Background" Value="lightgray"></Setter>  
                                                </Trigger>  
                                                <Trigger Property="IsMouseOver" Value="True">  
                                                    <Setter Property="Background" Value="#FFCBE3FE"></Setter>  
                                                </Trigger>  
                                            </Style.Triggers>  
                                        </Style>  
                                    </TextBlock.Style>  
                                </TextBlock>  
                            </Border>  
                        </ControlTemplate>  
                          
                    </Setter.Value>  
                </Setter>  
            </Style>  
        </Window.Resources>  
        <StackPanel>  
            <DataGrid Name="dg" AutoGenerateColumns="False">  
                <DataGrid.Columns>  
                    <DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}"/>  
                    <DataGridComboBoxColumn Header="colm" DisplayMemberPath="ItemName" Width="120">  
                        <DataGridComboBoxColumn.ElementStyle>  
                            <Style TargetType="{x:Type ComboBox}">  
                                <Setter Property="IsReadOnly" Value="{Binding IsUpdate}" />  
                                <Setter Property="ItemsSource" Value="{Binding Models}" />  
                                <Setter Property="SelectedIndex" Value="0"></Setter>  
                                <Style.Triggers>  
                                    <Trigger Property="IsReadOnly" Value="True">  
                                        <Setter Property="Foreground" Value="LightCyan"></Setter>  
                                        <Setter Property="IsEnabled" Value="False"></Setter>  
                                        <Setter Property="IsHitTestVisible" Value="False"></Setter>  
                                    </Trigger>  
                                </Style.Triggers>  
                            </Style>  
                        </DataGridComboBoxColumn.ElementStyle>  
                        <DataGridComboBoxColumn.EditingElementStyle>  
                            <Style TargetType="{x:Type ComboBox}">  
                                <Setter Property="IsReadOnly" Value="{Binding IsUpdate}" />  
                                <Setter Property="SelectedIndex" Value="0"></Setter>  
                                <Setter Property="ItemsSource" Value="{Binding Models}" />  
                                <Style.Triggers>  
                                    <Trigger Property="IsReadOnly" Value="True">  
                                        <Setter Property="Foreground" Value="lightgray"></Setter>  
                                        <Setter Property="IsEnabled" Value="False"></Setter>  
                                        <Setter Property="IsHitTestVisible" Value="False"></Setter>  
                                    </Trigger>  
                                </Style.Triggers>  
                            </Style>  
                        </DataGridComboBoxColumn.EditingElementStyle>  
                    </DataGridComboBoxColumn>  
                </DataGrid.Columns>  
            </DataGrid>  
            
        </StackPanel>  
    

    C# code is:

       public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
                List<Model> models1 = new List<Model>()  
                {  
                    new Model(){ItemName="Item1",IsBool=true},  
                    new Model(){ItemName="Item2",IsBool=false},  
                    new Model(){ItemName="Item3",IsBool=false},  
                    new Model(){ItemName="Item4",IsBool=true},  
                    new Model(){ItemName="Item5",IsBool=true},  
                };  
      
                List<Model> models2= new List<Model>()  
                {  
                    new Model(){ItemName="Item11",IsBool=true},  
                    new Model(){ItemName="Item22",IsBool=true},  
                    new Model(){ItemName="Item33",IsBool=false},  
                    new Model(){ItemName="Item55",IsBool=false},  
                    new Model(){ItemName="Item44",IsBool=false},  
                };  
      
                List<Person> people = new List<Person>()  
                {  
                    new Person(){Name="Jack",IsUpdate="True",Models = models1},  
                    new Person(){Name="Hohn",IsUpdate="False", Models = models2},  
                    new Person(){Name="John",IsUpdate="False", Models = models2},  
                };  
                dg.ItemsSource = people;  
            }  
        }  
      
        public class Person  
        {  
            public string Name { get; set; }  
      
            public string IsUpdate { get; set; }  
            public List<Model> Models { get; set; }  
        }  
      
        public class Model  
        {  
            public string ItemName { get; set; }  
            public bool IsBool { get; set; }  
        }  
    

    Result picture is:
    108858-3.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful