简体   繁体   中英

Use Telerik ComboBox in WPF default DataGrid as DataTemple Column binding problem

I prepared a WPF DataGrid and want to use a RadComboBox in a template column but the ItemsSource binding for MVVM does not work.

Is there any solution for that?

<DataGridTemplateColumn>
   <DataGridTemplateColumn.CellEditingTemplate>
      <DataTemplate>
         <telerik:RadComboBox ItemsSource="{Binding DataContext.Grades}"  />
      </DataTemplate>
   </DataGridTemplateColumn.CellEditingTemplate>
   <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <telerik:RadComboBox  ItemsSource="{Binding DataContext.Grades}"   />
      </DataTemplate>
   </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

If Grades is a collection property in your data grid items, remove DataContext .

<telerik:RadComboBox ItemsSource="{Binding Grades}"/>

If you have Telerik controls anyway, why not use its data grid equivalent RadGridView directly? It will automatically use its own RadComboBox control if you create a combo box column in it, eg:

<telerik:RadGridView ItemsSource="{Binding MyItemsCollection}" AutoGenerateColumns="False">
   <telerik:RadGridView.Columns>
      <telerik:GridViewComboBoxColumn Header="Grades"
                                      DataMemberBinding="{Binding SelectedGrade}"
                                      ItemsSourceBinding="{Binding Grades}"/>
   </telerik:RadGridView.Columns>
</telerik:RadGridView>

As a notice, you must have a property like SelectedGrade that stores the selected combo box item in your item type, because if you do not set a DataMemberBinding , the column will not show anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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