WPF DataGrid组合框

时间:2010-04-21 19:06:03

标签: c# wpfdatagrid

我想在数据网格中显示一个下拉列表,其中包含用户可以选择的不同值。不知何故,我无法显示ComboBox,而只是显示值,就好像它是一个普通的TextColumn。以下是我的代码,

<StackPanel Grid.Row="0">
            <toolkit:DataGrid Name="definitionGrid" Margin="0,10,0,0" AutoGenerateColumns="False" 
                                              CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True"
                                              RowHeight="25" FontWeight="Normal" ItemsSource="{Binding Profile}"
                                              SelectionMode="Single" ScrollViewer.HorizontalScrollBarVisibility="Auto" Width="450"
                              ScrollViewer.VerticalScrollBarVisibility="Auto" Height="200">
                <toolkit:DataGrid.Columns>
                    <toolkit:DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name}" CellStyle="{StaticResource cellCenterAlign}"/>
                    <toolkit:DataGridComboBoxColumn Header="Gender" Width="220" SelectedItemBinding="{Binding Gender}" ItemsSource="{Binding Source={StaticResource GenderValues}}" CellStyle="{StaticResource cellCenterAlign}"/>
                    <toolkit:DataGridCheckBoxColumn Header="Email" Width="60" Binding="{Binding ReceivesEmail}" CellStyle="{StaticResource cellCenterAlign}"/>
                    <toolkit:DataGridTextColumn Header="Others" Width="80" CellStyle="{StaticResource cellCenterAlign}"/>
                </toolkit:DataGrid.Columns>
            </toolkit:DataGrid>   
       </StackPanel>

1 个答案:

答案 0 :(得分:3)

DataGridComboBoxColumn只显示ComboBox,如果它处于编辑模式;否则它显示为普通的DataGridTextColumn。 如果要显示ComboBox,可以定义DataGridTemplateColumn:

<DataGridTemplateColumn Header="Gender">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Width="220" SelectedItem="{Binding Gender}" ItemsSource="{Binding Source={StaticResource GenderValues}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>