Databinding within Cell templated Devexpress:GridControl not working

依然范特西╮ 提交于 2019-12-11 16:01:38

问题


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>
...


来源:https://stackoverflow.com/questions/28783582/databinding-within-cell-templated-devexpressgridcontrol-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!