我试过你的例子,它工作正常:
XAML:
<Window x:Class="DataGridBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding ObjectAList}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{ Binding ObjectB.strDes}" Header=" Libellé type évènement " Width="Auto" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
代码隐藏:
public partial class MainWindow : Window
{
public List<A> AList { get; set; }
public MainWindow()
{
InitializeComponent();
AList= new List<A>();
AList.Add(new A());
AList.Add(new A());
AList.Add(new A());
AList.Add(new A());
DataContext = this;
}
}
A类和B类:
public class A
{
public B B { get; set; }
public A()
{
B=new B();
}
}
public class B
{
private static int counter = 0;
public string strDes { get; set; }
public B()
{
strDes = "foo"+counter;
counter++;
}
}
输出:
编辑:检查绑定的提示。将 TraceLevel 设置为“High”以获取有关它的更多信息:
<DataGridTextColumn Binding="{ Binding B.strDes, PresentationTraceSources.TraceLevel=High}" Header=" Libellé type évènement " Width="Auto" IsReadOnly="True" />