将 ObservableCollection 绑定到 wpf 数据网格:网格保持为空 [英] Bind an ObservableCollection to a wpf datagrid : Grid stays empty

查看:29
本文介绍了将 ObservableCollection 绑定到 wpf 数据网格:网格保持为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 ObservableCollection 绑定到 wpf 数据网格.我的 ObservableCollection 不为空,但是,我的数据网格保持为空:

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>

另见此答案

这篇关于将 ObservableCollection 绑定到 wpf 数据网格:网格保持为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆