【问题标题】:How to bind dynamic data to data grid in wpf?如何将动态数据绑定到 wpf 中的数据网格?
【发布时间】:2016-09-22 12:26:48
【问题描述】:

我有以下类作为我的数据模型:

public class InspectorOutput
{
    string m_SymbolName;
    // Each string is a column name with its value in double
    List<KeyValuePair<string, double>> m_ListPrices = new List<KeyValuePair<string, double>>();

    public string SymbolName
    {
        get
        {
            return m_SymbolName;
        }

        set
        {
            m_SymbolName = value;
        }
    }

    public List<KeyValuePair<string, double>> ListPrices
    {
        get
        {
            return m_ListPrices;
        }

        set
        {
            m_ListPrices = value;
        }
    }
    public void addResult(string strResultName, double nResult)
    {
        ListPrices.Add(new KeyValuePair<string, double>(strResultName, nResult));
    }
}

在我的 xaml 窗口中,数据网格定义如下:

<DataGrid x:Name="gridStockData">
                </DataGrid>

稍后,在我的主窗口中,我有以下代码:

    private void runProfile(object sender, RoutedEventArgs e)
    {
        ObservableCollection<InspectorOutput> listOutput = null;
        Profile.Profile objProfile = null;
        Inspector.InspectorManager objInspectorManager = null;
        try
        {
            // Some code here which makes a profile out of user input in objProfile
            objInspectorManager = new Inspector.InspectorManager();
            // Calculate data based on given profile
            listOutput = objInspectorManager.startInspector(objProfile);
            // Show calculated data
            gridStockData.ItemsSource = listOutput;
        }
        catch (Exception ex)
        {
            Logger.getInstance().error(ex.getStackTrace());
        }
    }

问题如下:

  • 我有 10 家公司的股票数据。
  • 每个公司都有一个符号名称。
  • 每个符号的计算数据存储在 m_ListPrices 中,其中每个键是列名,每个值是单元格值
    • 注意:直到运行时才能知道列(即:根据用户选择的算法,列名称和编号可能会有所不同)。

我有一个计算器类,可以运行用户选择的算法。每个算法都有自己的输出,将其存储在上述数据模型中。 我怎么可能将此 DataModel 绑定到 WPF 中的 DataGrid?

目前上面的代码给了我以下输出:

【问题讨论】:

  • 通过查看您提供的图像,我假设您说的是 Row* 而不是列:“// 每个字符串都是一个列名,其值为 double"
  • 考虑为每个用户选择的算法创建一个row details template。创建template selector 以根据所选行显示正确的模板。
  • 或者你可以做一个绑定到字典的通用数据模板。 Here 是一个 SO 示例。
  • 感谢您的快速回复。我发现行详细信息非常有用。

标签: c# wpf xaml data-binding


【解决方案1】:

这就是我解决问题的方法: 我为我的数据网格创建了一个 DataTable 作为数据源,其中填充了字典中的列。

                DataTable tbl = new DataTable();
            tbl.Columns.Add("Symbol Name", typeof(string));

            // Add columns
            foreach (InspectorOutput item in listScenarioOutput)
            {
                foreach (KeyValuePair<string, double> entry in item.ListPrices)
                {
                    tbl.Columns.Add(entry.Key, typeof(double));
                }
                break;
            }

            for (int i = 0; i < listScenarioOutput.Count; i++)
            {
                DataRow row = tbl.NewRow();
                row.SetField(0, listScenarioOutput[i].SymbolName);
                int j = 0;
                foreach (KeyValuePair<string, double> entry in listScenarioOutput[i].ListPrices)
                {
                    row.SetField(++j, entry.Value);
                }
                tbl.Rows.Add(row);
            }

            gridStockData.ItemsSource = tbl.DefaultView;

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 2011-05-28
    • 1970-01-01
    • 2011-01-31
    • 2017-05-14
    • 2013-04-15
    • 2015-08-07
    • 2012-11-21
    • 1970-01-01
    相关资源
    最近更新 更多