无法在 DataGrid 中显示记录

发布于 2024-12-12 19:31:22 字数 2064 浏览 1 评论 0原文

我是 WPF 的新手,在显示我的记录时遇到问题。尽管我已经拥有了所有记录,但在显示它们时,我的记录似乎“害羞”。

我的 App.xaml.cs 代码:

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        Window1 window = new Window1();
        var viewModel = new Window1ViewModel();        

        window.PHGrid.ItemsSource = viewModel.ViewModels;
        window.Show();
    }

Window1ViewModel 代码:

public class Window1ViewModel : ViewModelBase
{
    private readonly DAPHContrib _contribRepository;
    private ObservableCollection<ViewModelBase> _viewModelBases;

    public ObservableCollection<ViewModelBase> ViewModels
    {
        get
        {
            if (_viewModelBases == null)
            {
                _viewModelBases = new ObservableCollection<ViewModelBase>();
            }
            return _viewModelBases;
        }
    }

    public Window1ViewModel()
    {
        _contribRepository = new DAPHContrib();
        //Create instance of our view model to add it in our collection

        PHContribViewModel viewModel = new PHContribViewModel(_contribRepository);
        ViewModels.Add(viewModel);
    }
}

这是我的 Window1.xaml 更新

<Window x:Class="Wabby_App.Views.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:vm="clr-namespace:Wabby_App.ViewModels" 
    xmlns:v="clr-namespace:Wabby_App.Views" 
    Title="Utos ng mahal ko" 
    Height="300" 
    Width="300">

<Grid>
    <DataGrid 
        AutoGenerateColumns="True" 
        Height="200" 
        HorizontalAlignment="Center" 
        Name="PHGrid" 
        VerticalAlignment="Center" 
        Width="200"
        ItemsSource="{Binding ViewModels}"/>

</Grid>

输出:

我的记录是“害羞”

希望你能帮助我。

I am just new to WPF and I am having problems displaying my record. It seems that my records are "shy" when it comes to displaying them, even though I have all my records already.

Code for my App.xaml.cs:

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        Window1 window = new Window1();
        var viewModel = new Window1ViewModel();        

        window.PHGrid.ItemsSource = viewModel.ViewModels;
        window.Show();
    }

Code for Window1ViewModel:

public class Window1ViewModel : ViewModelBase
{
    private readonly DAPHContrib _contribRepository;
    private ObservableCollection<ViewModelBase> _viewModelBases;

    public ObservableCollection<ViewModelBase> ViewModels
    {
        get
        {
            if (_viewModelBases == null)
            {
                _viewModelBases = new ObservableCollection<ViewModelBase>();
            }
            return _viewModelBases;
        }
    }

    public Window1ViewModel()
    {
        _contribRepository = new DAPHContrib();
        //Create instance of our view model to add it in our collection

        PHContribViewModel viewModel = new PHContribViewModel(_contribRepository);
        ViewModels.Add(viewModel);
    }
}

Here's my Window1.xaml UPDATED:

<Window x:Class="Wabby_App.Views.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:vm="clr-namespace:Wabby_App.ViewModels" 
    xmlns:v="clr-namespace:Wabby_App.Views" 
    Title="Utos ng mahal ko" 
    Height="300" 
    Width="300">

<Grid>
    <DataGrid 
        AutoGenerateColumns="True" 
        Height="200" 
        HorizontalAlignment="Center" 
        Name="PHGrid" 
        VerticalAlignment="Center" 
        Width="200"
        ItemsSource="{Binding ViewModels}"/>

</Grid>

Output:

My records are "shy"

Hope you can help me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾其所爱 2024-12-19 19:31:22

根据您的评论,您有视图模型集合 (ObservableCollection),

并且在每个视图模型基本实例 (PHContribViewModel) 内,您有另一个集合 ObservableCollection<; PHContrib_Entity>

因此,您有两层嵌套集合和一个要映射的数据网格。这不会按原样工作。为此,您需要将这 2 级集合层次结构展平为 ObservableCollection 类型的一个列表。

使用 LINQ 来做到这一点...

 protected override void OnStartup(StartupEventArgs e) {
    base.OnStartup(e);
    Window1 window = new Window1();
    var viewModel = new Window1ViewModel();
    window.PHGrid.ItemsSource
     = viewModel.ViewModels.SelectMany(vm => vm.PHContribEntities).ToList();
    window.Show();
} 

让我知道这是否有帮助...

based on your comments you have view models collection (ObservableCollection<ViewModelBase>)

and inside each of these view model base instances (PHContribViewModel) you have another collection ObservableCollection<PHContrib_Entity>.

Hence you have two levels of nested collections and one datagrid to map. This wont work as it is. For this you would need to flatten this 2 level hierarchy of collections into one list of type ObservableCollection<PHContrib_Entity>.

Use LINQ to do that...

 protected override void OnStartup(StartupEventArgs e) {
    base.OnStartup(e);
    Window1 window = new Window1();
    var viewModel = new Window1ViewModel();
    window.PHGrid.ItemsSource
     = viewModel.ViewModels.SelectMany(vm => vm.PHContribEntities).ToList();
    window.Show();
} 

Let me know if this helps...

待"谢繁草 2024-12-19 19:31:22

您的视图 (Window1) 未绑定到 ViewModel,它只是将控件的 ItemsSource 设置为 ViewModel 的属性,这是实现 MVVM 的错误方法。您需要做的是将 DataContext 设置为Window1 到 ViewModel 的实例(将 View 绑定到 ViewModel)。因此,您需要更新 OnStartup 方法中的代码。

from

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    Window1 window = new Window1();
    var viewModel = new Window1ViewModel();        

    window.PHGrid.ItemsSource = viewModel.ViewModels;
    window.Show();
}

to

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    Window1 window = new Window1();
    var viewModel = new Window1ViewModel();        

    window.DataContext = viewModel;
    window.Show();
}

Update

您还需要将 datagrid 的 ItemsSource 属性设置为 ViewModel 中的属性

<DataGrid ItemsSource={Binding ViewModels} ..

Your View (Window1) is not binding to ViewModel, it's just setting the control's ItemsSource to a property of the ViewModel which is an incorrect way to implement MVVM. What you need to do is set DataContext of Window1 to instance of ViewModel (Bind View to ViewModel). So, you need to update your code in the OnStartup method.

from

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    Window1 window = new Window1();
    var viewModel = new Window1ViewModel();        

    window.PHGrid.ItemsSource = viewModel.ViewModels;
    window.Show();
}

to

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    Window1 window = new Window1();
    var viewModel = new Window1ViewModel();        

    window.DataContext = viewModel;
    window.Show();
}

Update

You also need to set ItemsSource property of datagrid to property in ViewModel

<DataGrid ItemsSource={Binding ViewModels} ..
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文