【问题标题】:Databinding within Cell templated Devexpress:GridControl not working单元格模板化 Devexpress 中的数据绑定:GridControl 不起作用
【发布时间】:2015-02-28 15:47:16
【问题描述】:

我尝试将类对象 StrategySubscription 中的 List 属性 SubscribedSymbols 作为 List 的一部分绑定到 Devexpress GridControl 中特定列的每个单元格中的组合框,但无法使数据绑定起作用。

自动列生成器工作并将值填充到网格上。所以,我确信数据存在。

我附上了 xaml 代码和数据对象以及当前输出的屏幕截图。

您能否帮助使数据绑定正常工作?我希望将 SubscribedSymbols 中的字符串集合填充到模板列中每个单元格的组合框中。

P.S.:前 3 个网格列和关联单元格绑定得很好,唯一的问题在于将数据绑定到最后一列每个单元格中的组合框。

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>

【问题讨论】:

    标签: c# wpf grid devexpress gridcontrol


    【解决方案1】:

    Data 添加到您的绑定中:

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

    如果您使用 DE 控件,更好的选择是使用 dxe:ComboBoxEdit 而不是 ComboBox

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

    【讨论】:

    • 它有效,纯粹的魔法:-) 但背后的想法是什么?为什么 Data.* 解决了绑定问题?你能帮我理解吗?非常感谢
    • @MattWolf 属性 Data 解决了绑定问题,因为在这种情况下,ComboBoxDataContextEditGridCellData,其中属性 Data 包含当前行数据
    • 非常感谢。我想我很难理解 DataContext 适用于组合框的 itemssource 的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 2019-04-14
    • 1970-01-01
    • 2015-10-07
    相关资源
    最近更新 更多