“DataContext数据上下文”绑定不上 , Why???
“DataContext数据上下文”绑定不上 , Why???
思路:
1、将“数据集类的实例对象”赋值给“Grid”的数据上下文,DataContext。
2、“Grid”里有一个“Lable”,一个“DataGrid”进行数据绑定。
问题:数据绑定不上。
1、后台代码:
public partial class CustomReportTest : UserControl
{
public CustomReportTest()
{
InitializeComponent();
EnergyList energylist = new EnergyList();
LayoutRoot.DataContext = energylist; //dg_EnergyCustom.ItemsSource = energylist.energylist;
}
}
public class EnergyList{
private string _energytitle;
public string EnergyTitle
{
get {return _energytitle;}
set { _energytitle = "指标报表"; }
}
public List<EnergyTarget> energylist = new List<EnergyTarget> { new EnergyTarget{EnergyYear="2010",EnergySum="7678.09",EnergyEveryOne="89.78",EnergyUnitArea="45.65"},
new EnergyTarget{EnergyYear="2011",EnergySum="8978.48",EnergyEveryOne="67.75",EnergyUnitArea="43.63"}
};
}
public class EnergyTarget{
public string EnergyYear
{
get;
set;
}
public string EnergySum
{
get;
set;
}
public string EnergyUnitArea
{
get;
set;
}
public string EnergyEveryOne
{
get;
set;
}
}
2、XAML代码
<Grid x:Name="LayoutRoot" Background="White"> <Grid Margin="69,80,79,82">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition/>
</Grid.RowDefinitions>
<sdk:Label Background="#FFB7D4E0" Content="{Binding EnergyTitle}"/> <sdk:DataGrid Grid.Row="1" Name="dg_EnergyCustom" AutoGenerateColumns="False" ItemsSource="{Binding energylist}"> <sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn DisplayIndex="-1" Header="年份" IsReadOnly="True" Width="30*" >
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock FontSize="13" HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{Binding EnergyYear}"></TextBlock>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="项目" Width="30*">
<sdk:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Grid ShowGridLines="True" HorizontalAlignment="Stretch&