将一个ObservableCollection绑定到wpf datagrid:网格保持为空 [英] Bind an ObservableCollection to a wpf datagrid : Grid stays empty
本文介绍了将一个ObservableCollection绑定到wpf datagrid:网格保持为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想绑定一个 ObservableCollection
到wpf datagrid。
我的 ObservableCollection
不是空的,但是我的datagrid保持为空:
I would like to bind an ObservableCollection
to wpf datagrid.
My ObservableCollection
is not empty, but, my datagrid stay empty :
public partial class Fenetre_EvtCode : Window
{
ObservableCollection<EvtCode> glb_ObservableEvtCode;
public Fenetre_EvtCode()
{
InitializeComponent();
EvtCode myEvt = new EvtCode();
glb_ObservableEvtCode = myEvt.GetAllEvtCode();
}
}
这是我的xaml:
<DataGrid Foreground="Aqua"
Name="myDataGridEvtCode"
AutoGenerateColumns="True"
HorizontalAlignment="Stretch"
Margin="0,0,0,0"
VerticalAlignment="Stretch"
Height="453"
ItemsSource="{Binding glb_ObservableEvtCode}" />
我重复:我看了调试,我的 ObservableCollection
不是空的。
I repeat : I looked in debug, and my ObservableCollection
is not empty.
任何人都知道为什么ma datagrid保持空白?
Anyone know why ma datagrid stay empty?
推荐答案
您需要绑定到公共属性。
public ObservableCollection<EvtCode> ObservableEvtCode
{
get
{
return this.glb_ObservableEvtCode;
}
}
和XAML:
<DataGrid
...
DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
ItemsSource="{Binding ObservableEvtCode}" >
</DataGrid>
编辑:另请参见answer
这篇关于将一个ObservableCollection绑定到wpf datagrid:网格保持为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文