簡體   English   中英

如果綁定到的DataTable具有項目列表,如何在DataGrid中顯示ComboBox?

[英]How to display a ComboBox in a DataGrid if the DataTable it is bound to has a list of items?

我有一個數據集。 我不知道布景的內容。 我只需要在DataGrid中顯示集合的表即可。 我可以使用以下代碼來做到這一點。 為了能夠使用它,我創建了自己的數據集。 CustomerDataProvider是我創建的類,該類具有一種返回偽DataSet的方法。

CustomerDataProvider provider = new CustomerDataProvider();
DataSet ds = new DataSet();
DataTable table = new DataTable();
DataView view = new DataView();



    public MainWindow()
    {
        InitializeComponent();
        ds = dataset.GetDataSet();
        table = ds.Tables[0];
        view = table.AsDataView();
        this.DataContext = view;
    }


<Grid>
    <DataGrid x:Name="dynamicGrid" ItemsSource="{Binding Path=., Mode=TwoWay}" ColumnWidth="*"  />
</Grid>

現在,如果DataTable包含布爾值,則DataGrid將自動顯示一個CheckBox。 如果DataTable包含項目列表,我希望能夠自動顯示ComboBox。 我該如何實現這一目標?

代替自動生成列,可以將AutoGenerateColumns設置為false,您可以定義列並與DataTable不同字段綁定。 您可以使用DataGridTemplateColumn並為其提供CellTemplate 請參閱下面的參考代碼:

  <DataGrid AutoGenerateColumns="False">
    <DataGridTemplateColumn>
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <ComboBox />
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM