本文介绍了单元格模板中的数据绑定Devexpress:GridControl不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我尝试在类对象StrategySubscription中将List属性SubscribedSymbols绑定到Devexpress GridControl中特定列的每个单元格中的列表到组合框中,但不能使数据绑定正常工作。

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



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



你能帮助数据绑定工作正常吗?我想要在SubscribedSymbols中的字符串集合在模板列中的每个单元格的组合框中填充。



PS:前3个网格列和关联单元格完全绑定,唯一的问题在于将数据绑定到最后一列的每个单元格中的组合框。

  public class StrategySubscription 
{
public Guid StrategyId {get;组; }
public string StrategyName {get;组; }
public int CapitalAllocation {get;组; }
public List< string>订阅的符号{get;组;

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




< dxg:GridControl x:Name =StrategyGridItemsSource ={Binding StrategySubscriptions}的AutoGenerateColumns = 无 >
< dxg:GridControl.Columns>
< dxg:GridColumn Header =策略IDBinding ={Binding StrategyId}/>
< dxg:GridColumn Header =策略名称Binding ={Binding StrategyName}/>
< dxg:GridColumn Header =策略资本化Binding ={Binding CapitalAllocation}/>
< dxg:GridColumn Header =符号订阅>
< dxg:GridColumn.CellTemplate>
< DataTemplate>
< ComboBox ItemsSource ={Binding SubscribedSymbols}/>
< / DataTemplate>
< / dxg:GridColumn.CellTemplate>
< / dxg:GridColumn>
< / dxg:GridControl.Columns>

< dxg:GridControl.View>
< dxg:TableView AllowEditing =FalseAutoWidth =TrueBestFitArea =AllAllowBestFit =TrueShowGroupPanel =TrueShowSearchPanelMode =AlwaysNavigationStyle =Row/>
< / dxg:GridControl.View>
< / dxg:GridControl>


解决方案

添加数据到你的绑定:

  ... 
< dxg:GridColumn Header =符号订阅 >
< dxg:GridColumn.CellTemplate>
< DataTemplate>
< ComboBox ItemsSource ={Binding Data.SubscribedSymbols}/>
< / DataTemplate>
< / dxg:GridColumn.CellTemplate>
< / dxg:GridColumn>
...

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

  ... 
< dxg:GridColumn Header =符号订阅>
< dxg:GridColumn.CellTemplate>
< DataTemplate>
< dxe:ComboBoxEdit
ItemsSource ={Binding Data.SubscribedSymbols}/>
< / DataTemplate>
< / dxg:GridColumn.CellTemplate>
< / dxg:GridColumn>
...


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>
解决方案

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

这篇关于单元格模板中的数据绑定Devexpress:GridControl不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:47