我想在wpf的dataGrid列中填充嵌套数据,并且我已经在主类模型上附加了datagrid itemsource,所以我不知道如何使用和填充其中的详细信息。
public class SESSION
{
public string open { get; set; }
public string high { get; set; }
public string low { get; set; }
public string last { get; set; }
}
public class QUOTE
{
public string symbol { get; set; }
public string name { get; set; }
public List<SESSION> SESSION { get; set; }
}
public class RootObject
{
public List<QUOTE> QUOTE { get; set; }
}
我的datagrid在xaml文件中是:
<DataGrid Name="grdData">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding name}" />
<DataGridTextColumn Header="Symbol" Binding="{Binding symbol}"/>
<DataGridTextColumn Header="High" Binding="{Binding high}" />
<DataGridTextColumn Header="Low" Binding="{Binding low}" />
</DataGrid.Columns>
</DataGrid>
我也在使用grdData.ItemsSource = rootObject.QUOTE.ToList();用于在gridview中填充数据。 那么如何从DataGrid的会话中获取开盘价,最高价,最低价和最后一个值?
请帮助解决这一问题。
答案 0 :(得分:0)
由于SESSION属性是一个列表,因此您需要使用其他集合控件,例如在DataGridTemplateColumn中的ItemsControl,或绑定到列表中的特定项目。从评论的角度来看,与后者一起使用,我有一个如下的测试窗口:
XAML:
myScore.getInt ("score_value", 0);
后面的代码:
<Window x:Class="TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CoSecApp_v3"
mc:Ignorable="d"
Title="TestWindow" Height="450" Width="800">
<Grid>
<DataGrid Name="grdData" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding name}" />
<DataGridTextColumn Header="Symbol" Binding="{Binding symbol}"/>
<DataGridTextColumn Header="High" Binding="{Binding Path=SESSION[0].high}" />
<DataGridTextColumn Header="Low" Binding="{Binding Path=SESSION[0].low}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
这将按预期的测试数据填充我的网格。
(顺便说一句,public class TestWindow
{
private void TestWindow_Loaded(object sender, RoutedEventArgs e)
{
RootObject DC = new RootObject();
DC.QUOTE = new List<QUOTE>();
for (int I = 1; I <= 10; I++)
{
DC.QUOTE.Add(new QUOTE() { name = "Name" + I, symbol = I, SESSION = new List<SESSION>() });
DC.QUOTE[I - 1].SESSION.Add(new SESSION() { open = "open", high = "100", low = "10", last = "final" });
}
grdData.ItemsSource = DC.QUOTE.ToList();
}
public class SESSION
{
public string open { get; set; }
public string high { get; set; }
public string low { get; set; }
public string last { get; set; }
}
public class QUOTE
{
public string symbol { get; set; }
public string name { get; set; }
public List<SESSION> SESSION { get; set; }
}
public class RootObject
{
public List<QUOTE> QUOTE { get; set; }
}
}
上不需要.ToList,因为它已经是一个List对象。)