Search code examples
c#wpfxamlcontextmenudatagridcomboboxcolumn

WPF DataGridComboBoxColumn not showing the context menu?


I am trying to make a DataGridComboBoxColumn that has a static list to choose from: Not Started, In Progress, Completed

Here is what I have in the XAML and while it builds fine I cannot see the options in the drop down:

           <DataGridComboBoxColumn  Header="Status" Width="auto"  IsReadOnly="False"  >
                <DataGridColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader">
                        <Setter Property="Background" Value="LightGoldenrodYellow" />
                        <Setter Property="BorderThickness" Value="2,2,0,2" />
                    </Style>

                </DataGridColumn.HeaderStyle>
                <ContextMenuService.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Not Started" />
                        <MenuItem Header="In Progress"  />
                        <MenuItem Header="Completed" />  
                    </ContextMenu>
                </ContextMenuService.ContextMenu>
            </DataGridComboBoxColumn>

I don't understand why this doesn't work they way that other DataGrid.ContextMenu's work. I feel like I'm missing something really easy here.


Solution

  • you can use CompositeCollection:

    <DataGridComboBoxColumn  Header="Status" Width="auto"  IsReadOnly="False"  SelectedItemBinding="{Binding Path=Value}">
                    <DataGridColumn.HeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="Background" Value="LightGoldenrodYellow" />
                            <Setter Property="BorderThickness" Value="2,2,0,2" />
                        </Style>
                    </DataGridColumn.HeaderStyle>
                    <DataGridComboBoxColumn.ItemsSource>
                        <CompositeCollection>
                            <sys:String>Not Started</sys:String>
                            <sys:String>In Progress</sys:String>
                            <sys:String>Completed</sys:String>
                        </CompositeCollection>
                    </DataGridComboBoxColumn.ItemsSource>
                </DataGridComboBoxColumn>
    

    Add the namespace:

    xmlns:sys="clr-namespace:System;assembly=mscorlib"