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

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

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

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

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

<DataGrid ItemsSource="{Binding StrategySubscriptions}" AutoGenerateColumns="False">

                    <DataGrid.Columns>

                        <DataGridTextColumn Header="Strategy ID" Binding="{Binding StrategyId}"/>
                        <DataGridTextColumn Header="Strategy Name " Binding="{Binding StrategyName}"/>
                        <DataGridTextColumn Header="Strategy Capitalization" Binding="{Binding CapitalAllocation}"/>
                        <DataGridComboBoxColumn Header="Symbol Subscriptions" ItemsSource="{Binding SubscribedSymbols, RelativeSource={RelativeSource TemplatedParent}}"/>

                    </DataGrid.Columns>

                </DataGrid>

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));
        }
    }

【问题讨论】:

    标签: c# wpf data-binding datagrid


    【解决方案1】:

    尝试使用模板列对其进行更多控制:

    <DataGridTemplateColumn Header="Symbol Subscriptions">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ComboBox ItemsSource="{Binding SubscribedSymbols}"/>
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    

    问题的原因是 DataGridColumns 不是可视化树的一部分。在此处查看更多说明:http://blogs.msdn.com/b/vinsibal/archive/2008/12/17/wpf-datagrid-dynamically-updating-datagridcomboboxcolumn.aspx

    【讨论】:

    • 谢谢你完美的工作。你介意看看以下吗?我尝试为 DevExpress Gridcontrol 完成相同的操作:stackoverflow.com/questions/28783582/…(P.S.:我最终必须按时间选择答案,因为我发现它们同样好)。
    【解决方案2】:

    试试这个:

    ...
    <DataGridComboBoxColumn Header="Symbol Subscriptions" >
        <DataGridComboBoxColumn.ElementStyle>
            <Style TargetType="{x:Type ComboBox}">
                <Setter Property="ItemsSource" Value="{Binding Path=SubscribedSymbols}" />
            </Style>
        </DataGridComboBoxColumn.ElementStyle>
        <DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="{x:Type ComboBox}">
                <Setter Property="ItemsSource" Value="{Binding Path=SubscribedSymbols}" />
            </Style>
        </DataGridComboBoxColumn.EditingElementStyle>
    </DataGridComboBoxColumn>
    ...
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 2017-11-05
    • 2016-05-22
    • 2016-03-26
    相关资源
    最近更新 更多