【发布时间】: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