欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF在Devexpress的gridcontrol中ComboBox绑定数据

程序员文章站 2022-06-07 18:34:54
...

 

 

 

 

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:

WPF在Devexpress的gridcontrol中ComboBox绑定数据

 

其中{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>