除非绑定模式更改,否则WPF DataGrid数据不显示

8e2ybdfx  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(51)

我们有一个绑定到对象“Test”的ObservableCollection的DataGrid,其中Test继承自INotifyPropertyChanged。然而,尽管ObservableCollection被填充并且OnPropertyChanged正在运行,DataGrid在运行时为空。
为了让事情变得更加混乱,我们有一个非常类似的DataGrid,以相同的方式设置,但数据稍微复杂一些,并且工作正常。此外,如果我们在调试时更改绑定模式,数据会出现在表中,并且可以按照我们的预期进行更新。
下面复制了DataGrid的Xaml和ObservableCollection属性

<DataGrid Name="NonFunctioningTable" ItemsSource="{Binding PopulatedList, Mode=TwoWay}" Height="388" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="false" ColumnWidth="*" Margin="0,10,0,0">
<DataGrid.Columns>
    <DataGridTextColumn Header="Column1" Binding="{Binding ObjectProperty1}" />
    <DataGridTextColumn Header="Column2" Binding="{Binding ObjectProperty2}" />
</DataGrid.Columns>
</DataGrid>

private ObservableCollection<Test> populatedList;
public ObservableCollection<Test> PopulatedList
{
    get => populatedList;
    set
    {
        populatedList = value;
        OnPropertyChanged();
    }
}

字符串

68de4m5k

68de4m5k1#

我们已经解决了这个问题。
MainWindow.cs中的一行是在开发之初添加的,在添加ViewModel之前,它将表的ItemSource设置为null,从而删除了绑定。

相关问题