简体   繁体   English

WPF DataGridComboBoxColumn不显示上下文菜单吗?

[英]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 我正在尝试使DataGridComboBoxColumn具有静态列表可供选择:未开始,进行中,已完成

Here is what I have in the XAML and while it builds fine I cannot see the options in the drop down: 这是XAML中我所拥有的,尽管它可以正常运行,但在下拉列表中看不到这些选项:

           <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. 我不明白为什么这不能像其他DataGrid.ContextMenu那样工作。 I feel like I'm missing something really easy here. 我觉得我在这里确实缺少一些简单的东西。

you can use CompositeCollection: 您可以使用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 ContextMenuService提供用于显示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. 当前设置为可用于单元格单击,但可以轻松设置为“页眉”或两者都设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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