gpt4 book ai didi

c# - 将 ObservableCollection 绑定(bind)到 wpf 数据网格 : Grid stays empty

转载 作者:太空狗 更新时间:2023-10-29 20:54:53 26 4
gpt4 key购买 nike

我想将一个 ObservableCollection 绑定(bind)到 wpf datagrid。我的 ObservableCollection 不为空,但是,我的数据网格保持为空:

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 不为空。

有人知道为什么 ma datagrid 保持空白吗?

最佳答案

您需要绑定(bind)到一个公共(public)属性

public ObservableCollection<EvtCode> ObservableEvtCode
{
get
{
return this.glb_ObservableEvtCode;
}
}

和 XAML:

<DataGrid  
...
DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
ItemsSource="{Binding ObservableEvtCode}" >
</DataGrid>

编辑:另见 answer

关于c# - 将 ObservableCollection 绑定(bind)到 wpf 数据网格 : Grid stays empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15087131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com