简体   繁体   English

使用Telerik ComboBox in WPF default DataGrid as DataTemple 列绑定问题

[英]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.我准备了一个 WPF DataGrid并想在模板列中使用RadComboBox ,但 MVVM 的ItemsSource绑定不起作用。

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 .如果Grades是数据网格项中的集合属性,请移除DataContext

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

If you have Telerik controls anyway, why not use its data grid equivalent RadGridView directly?如果你有 Telerik 个控件,为什么不直接使用它的数据网格等效RadGridView It will automatically use its own RadComboBox control if you create a combo box column in it, eg:如果您在其中创建组合框列,它将自动使用自己的RadComboBox控件,例如:

<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.请注意,您必须有一个像SelectedGrade这样的属性,用于在您的项目类型中存储所选的组合框项目,因为如果您不设置DataMemberBinding ,该列将不会显示任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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