Databinding within Cell templated Devexpress:GridC

2019-09-03 01:55发布

问题:

I try to bind a List property SubscribedSymbols within a class object StrategySubscription as part of List to comboboxes in each cells of a specific column in a Devexpress GridControl but cannot get the data binding to work.

The auto column generator works and populates values onto the grid. So, I am sure the data exists.

I attached the xaml code and data object as well as a screenshot of the current output.

Can you please help to get the data binding to work correctly? I want the collection of strings within SubscribedSymbols to be populated in the comboboxes of each cell in the templated column.

P.S.: The first 3 grid columns and associate cells bind perfectly fine, the only problem lies with getting the data bound to the combobox in each cell of the last column.

public class StrategySubscription
    {
        public Guid StrategyId { get; set; }
        public string StrategyName { get; set; }
        public int CapitalAllocation { get; set; }
        public List<string> SubscribedSymbols { get; set; }

        public StrategySubscription(string strategyName, Guid strategyId, int capitalAllocation, List<SymbolSubscription> symbolSubscriptions)
        {
            StrategyName = strategyName;
            StrategyId = strategyId;
            CapitalAllocation = capitalAllocation;
            SubscribedSymbols = symbolSubscriptions.Select(x => x.Symbol.SymbolId).ToList();
            //SubscribedSymbols = String.Join(", ", symbolSubscriptions.Select(x => x.Symbol.SymbolId).OrderBy(x=>x));
        }
    }




<dxg:GridControl x:Name="StrategyGrid"  ItemsSource="{Binding StrategySubscriptions}" AutoGenerateColumns="None">
                    <dxg:GridControl.Columns>
                        <dxg:GridColumn Header="Strategy Id" Binding="{Binding StrategyId}"/>
                        <dxg:GridColumn Header="Strategy Name" Binding="{Binding StrategyName}"/>
                        <dxg:GridColumn Header="Strategy Capitalization" Binding="{Binding CapitalAllocation}"/>
                        <dxg:GridColumn Header="Symbol Subscription">
                            <dxg:GridColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding SubscribedSymbols}"/>
                                </DataTemplate>
                            </dxg:GridColumn.CellTemplate>
                        </dxg:GridColumn>
                    </dxg:GridControl.Columns>

                    <dxg:GridControl.View>
                        <dxg:TableView AllowEditing="False" AutoWidth="True" BestFitArea="All" AllowBestFit="True" ShowGroupPanel="True" ShowSearchPanelMode="Always" NavigationStyle="Row"/>
                    </dxg:GridControl.View>
                </dxg:GridControl>

回答1:

Add Data to your binding:

...
<dxg:GridColumn Header="Symbol Subscription">
    <dxg:GridColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Data.SubscribedSymbols}"/>
        </DataTemplate>
    </dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
...

If you using DE controls the better option is using dxe:ComboBoxEdit instead of ComboBox.

...
<dxg:GridColumn Header="Symbol Subscription">
    <dxg:GridColumn.CellTemplate>
        <DataTemplate>
            <dxe:ComboBoxEdit  
                ItemsSource="{Binding Data.SubscribedSymbols}"/>
        </DataTemplate>
    </dxg:GridColumn.CellTemplate>
</dxg:GridColumn>
...