简体   繁体   中英

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.

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"

ContextMenuService provides the system implementation for displaying a ContextMenu

What you want is not too far off from what you already had. Its tested and works for me:

<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>
    <DataGridComboBoxColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="Not Started" />
                        <MenuItem Header="In Progress"  />
                        <MenuItem Header="Completed" />
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridComboBoxColumn.CellStyle>
</DataGridComboBoxColumn>

Currently set to work for cell clicking but can be easily set to Header or both.

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