2010-04-21 118 views
2

我想在數據網格中顯示一個下拉列表,用戶可以從中選擇不同的值。不知何故,我無法顯示ComboBox,只是將它顯示爲正常的TextColumn。以下是我的代碼,WPF DataGrid Combobox

<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> 

回答

3

DataGridComboBoxColumn只顯示ComboBox,如果它處於編輯模式;否則它顯示爲一個普通的DataGridTextColumn。 如果你想顯示一個組合框,你可以定義一個DataGridTemplateColumn:

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