Use the CellTemplate property to define a template that specifies the visualization of column cells. The binding source for the CellTemplate template is represented by the EditGridCellData class.
The EditGridCellData class has the Value property that provides access to the cell editable value. Use the RowData.Row property to access a row's data object property: {Binding RowData.Row.[YourPropertyName]}
The code sample belows shows how to use a custom ComboBoxEdit within grid cells:
其中{Binding RowData.Row.[YourPropertyName]} 绑定可以更换为其他的:
<ObjectDataProvider x:Key="SpectrometerState" MethodName="GetValues" ObjectType="{x:Type core:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type Type="Infrastructure:SpectrometerState"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
//然后再需要的地方添加绑定 ,x:Name=“Part_Editor”是必须的
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<dxe:ComboBoxEdit x:Name="PART_Editor" ItemsSource="{Binding Source={StaticResource SpectrometerState}}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
以下是官网提供的代码
public ObservableCollection<CountryCities> Items {
get {
if(this._Items == null) {
this._Items = new ObservableCollection<CountryCities>();
CountryCities usa = new CountryCities() {
Country = "USA",
Cities = new List<string> { "Washington, D.C.", "New York", "Los Angeles", "Las Vegas" },
City = "Los Angeles"
};
this._Items.Add(usa);
CountryCities germany = new CountryCities() {
Country = "Germany",
Cities = new List<string> { "Berlin", "Munich", "Frankfurt" },
City = "Munich"
};
this._Items.Add(germany);
CountryCities russia = new CountryCities() {
Country = "Russia",
Cities = new List<string> { "Moscow", "Saint-Petersburg" },
City = "Moscow"
};
this._Items.Add(russia);
}
return this._Items;
}
}
<dxg:GridControl AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" ItemsSource="{Binding Items}">
<dxg:GridColumn FieldName="Country"/>
<dxg:GridColumn FieldName="City">
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<dxe:ComboBoxEdit x:Name="PART_Editor" ItemsSource="{Binding RowData.Row.Cities}"/>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
<dxg:GridControl.View>
<dxg:TableView/>
</dxg:GridControl.View>
</dxg:GridControl>