将组合框添加到datagrid中的单元格

时间:2018-11-26 11:48:41

标签: c# wpf

我想向数据网格添加组合框,我的代码是

 <DataGrid>
    <DataGrid.Columns>
       <DataGridTextColumn Header="Catalog Number"  Width="1*"  />
       <DataGridTextColumn Header="Description"  Width="1*" />
    </DataGrid.Columns>
 </DataGrid>

我需要类似<ComboBox DataGridRow="1" DataGridColumn="1"/>

的内容

注意:我想将组合框添加到一个特定的单元格

2 个答案:

答案 0 :(得分:0)

Please try this Code

<DataGrid Name="dg" 
                  ItemsSource="{Binding DGRows}" 
                  AutoGenerateColumns="False"> 
            <DataGrid.Columns> 
                <DataGridTextColumn Binding="{Binding RowDescription}" Header="Description"/> 

                <DataGridComboBoxColumn  SelectedValueBinding="{Binding ComboCollectionId}" 
                                         SelectedValuePath="ComboRowId" 
                                         DisplayMemberPath="ComboRowDescription" 
                                         Header="Combo Column"> 
                    <DataGridComboBoxColumn.ElementStyle> 
                        <Style TargetType="ComboBox"> 
                            <Setter Property="ItemsSource" Value="{Binding DataContext.ComboRows, RelativeSource={RelativeSource AncestorType=Window}}" /> 
                            <Setter Property="IsReadOnly" Value="True"/> 
                        </Style> 
                    </DataGridComboBoxColumn.ElementStyle> 
                    <DataGridComboBoxColumn.EditingElementStyle> 
                        <Style TargetType="ComboBox"> 
                            <Setter Property="ItemsSource" Value="{Binding DataContext.ComboRows, RelativeSource={RelativeSource AncestorType=Window}}" /> 
                        </Style> 
                    </DataGridComboBoxColumn.EditingElementStyle> 
                </DataGridComboBoxColumn> 
            </DataGrid.Columns> 
        </DataGrid>

答案 1 :(得分:0)

如果您真的只想在一个单元格中有一个组合框,而不是整个列,则需要使用带有触发器的模板列来显示组合框。

<DataGrid>
   <DataGrid.Columns>
      <DataGridTemplateColumn Header="Catalog Number"  Width="1*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBlock Text="{Binding JobInfo.Jobname}">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Style.Triggers>
                                        <Setter Property="Visibility" Value="Visible"/>
                                        <DataTrigger Binding="{Binding IsComboBox}" Value="True">
                                            <Setter Property="Visibility" Value="Hidden"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>

                        <ComboBox>
                            <ComboBox.Style>
                                <Style TargetType="ComboBox">
                                    <Style.Triggers>
                                        <Setter Property="Visibility" Value="Hidden"/>
                                        <DataTrigger Binding="{Binding IsComboBox}" Value="True">
                                            <Setter Property="Visibility" Value="Visible"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ComboBox.Style>
                        </ComboBox>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
         </DataGridTemplateColumn>
      <DataGridTextColumn Header="Description"  Width="1*" />
   </DataGrid.Columns>
</DataGrid>