为两个DataGrid绑定两个ViewModel,但是只有1个DataGrid可以显示数据

Binding two ViewModel for two DataGrid, but only 1 DataGrid can display data

我有 2 个 DataGrid 来显示来自数据库 MySQL 的 2 个表的信息。我正在使用 MVVM 模式,并且我有 2 个 ViewModel 称为:"CheckJigInfoViewModel" 和 "AccessoryViewModel" 用于那些 DataGrid。我将那些 ViewModel 设置为我的两个 DataGrid,但只有二分之一的 DataGrid 可以显示数据。

我是不是弄错了一些代码?请多多指教!

我附上上面内容的一些图片和我的 ViewModel 的代码:

- ViewModel 代码:

   class CheckJigInfoViewModel
    {
        PersonnelBusinessObject personnel;
        ObservableCollection<CheckJigInfo> _listCheckJigInfo;
        public CheckJigInfoViewModel(){
            personnel = new PersonnelBusinessObject();
        }

        public ObservableCollection<CheckJigInfo> ListCheckJigInfo
        {
            get
            {
                _listCheckJigInfo = new ObservableCollection<CheckJigInfo>(personnel.GetListCheckJigInfo());
                return _listCheckJigInfo;
            }
        }

    }
class AccessoryViewModel
    {
        PersonnelBusinessObject personnel;
        ObservableCollection<Accessory> _lstAccessory;
        public AccessoryViewModel()
        {
            personnel = new PersonnelBusinessObject();
        }

        public ObservableCollection<Accessory> ListAccessory
        {
            get
            {
                _lstAccessory = new ObservableCollection<Accessory>(personnel.GetListAccessory());
                return _lstAccessory;
            }           
        }

    }
<Window x:Class="CheckJigWPF.View.AccessoryInfo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CheckJigWPF.View"
        xmlns:ViewModels="clr-namespace:CheckJigWPF.ViewModels"
        mc:Ignorable="d"
        Title="Accessory Info " Height="569.1" Width="890">
    <Window.Resources>

        <ViewModels:CheckJigInfoViewModel x:Key="CheckJigData"/>
        <ViewModels:AccessoryViewModel x:Key="AccessoryData"/>

    </Window.Resources>
    <!---MAIN GRID-->
    <Grid >
        <!---MAIN GRID ROWS AND COLUMNS DEFINITION-->
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>

        <!--GROUPBOX1-->
        <GroupBox Grid.Row="0" Header="Pulley checking info" Padding="0,2">
            <!--SUB GRID FOR GROUPBOX1-->
            <Grid>
                <!---SUB GRID ROWS AND COLUMNS DEFINITION-->
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="auto"/>
                </Grid.ColumnDefinitions>
                <!--GROUPBOX1's Controls-->
                <TextBox Grid.Column="0" Name="txtSearch" FontSize="24"/>
                <StackPanel Grid.Column="1" Orientation="Horizontal">
                    <Button Grid.Column="1" Name="btnSearch" Width="100" Content="Search"/>
                    <Button Grid.Column="1" Name="btnClear" Width="100" Content="Clear"/>
                </StackPanel>
                <DockPanel Grid.Row="1" Grid.ColumnSpan="2">
                    <DataGrid  AutoGenerateColumns="False" ItemsSource="{Binding ListCheckJigInfo, Source={StaticResource CheckJigData}}" ColumnWidth="*" SelectionMode="Single" IsReadOnly="True" FontSize="18" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="ID" Width="50" Binding="{Binding ID}"/>
                            <DataGridTextColumn Header="AUFNR" Width="200" Binding="{Binding AUFNR}"/>
                            <DataGridTextColumn Header="MATNR" Width="200" Binding="{Binding MATNR}"/>
                            <DataGridTextColumn Header="Shelf" Width="100" Binding="{Binding Shelf}"/>
                            <DataGridTextColumn Header="MAKTX" Width="*" Binding="{Binding MAKTX}"/>
                            <DataGridTextColumn Header="Qty" Width="100" Binding="{Binding Qty}"/>
                            <DataGridTextColumn Header="MEINS" Width="120" Binding="{Binding MEINS}"/>
                            <DataGridTextColumn Header="CheckStatus" Width="150" Binding="{Binding Check}"/>
                            <DataGridTextColumn Header="CheckingDate" Width="180" Binding="{Binding CheckingDate,StringFormat='yyyy-MM-dd HH:mm:ss'}"/>
                            <DataGridTextColumn Header="User" Width="100" Binding="{Binding UserID}"/>
                        </DataGrid.Columns>
                    </DataGrid>

                    <!--paging-->
                    <StackPanel Name="pager" Orientation="Horizontal">
                        <TextBlock Text="{Binding Page,StringFormat=Page 0 of 0}"/>
                    </StackPanel>
                </DockPanel>
                <!--End GROUPBOX1's Controls-->
            </Grid>
        </GroupBox>
        <!--END GROUPBOX1-->

        <!--GROUPBOX2-->
        <GroupBox Grid.Row="1" Header="Accessory Management" Padding="5">
            <!--SUB GRID FOR GROUPBOX2-->
            <Grid>
                <!--GROUPBOX2 ROWS AND COLUMNS DEFINITION-->
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <!--GROUPBOX2's Controls-->
                <DockPanel Grid.Row="0" Grid.Column="0">
                    <StackPanel DockPanel.Dock="Top">
                        <TextBlock Text="AccessoryID"  Margin="5" Padding="5" Background="#FFD7F4AA" FontSize="18"/>
                        <TextBox Name="txtAccessoryID"  Margin="5" FontSize="20"/>
                    </StackPanel>

                    <StackPanel DockPanel.Dock="Top">
                        <TextBlock Text="Accessory Name" Margin="5" Padding="5" Background="#FFD7F4AA" FontSize="18"/>
                        <TextBox Name="txtAccessoryName" Margin="5" FontSize="20"/>
                    </StackPanel>

                    <StackPanel DockPanel.Dock="Top">
                        <TextBlock Text="Shelf" Margin="5" Padding="5" Background="#FFD7F4AA" FontSize="18"/>
                        <TextBox Name="txtShelf" Margin="5" FontSize="20"/>
                    </StackPanel>
                </DockPanel>


                <!--SUB GRID FOR GROUPBOX2-->
                <Grid Grid.Column="1">
                    <!--GROUPBOX2 ROWS AND COLUMNS DEFINITION-->
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="auto"/>
                    </Grid.ColumnDefinitions>
                    <!--GROUPBOX2's Controls-->
                    <TextBox Grid.Column="0" Name="txtSearchAcs" FontSize="24"/>
                    <StackPanel Grid.Column="1" Orientation="Horizontal">
                        <Button  Name="btnSearchAcs" Width="100" Content="Search"/>
                        <Button  Name="btnClearAcs" Width="100" Content="Clear"/>
                    </StackPanel>
                    <DockPanel Grid.Row="1" Grid.ColumnSpan="2">
                        <DataGrid  Name="dgvAccessoryInfo" ItemsSource="{Binding ListAccessory, Source ={StaticResource AccessoryData}}"  AutoGenerateColumns="False" ColumnWidth="*" SelectionMode="Single" IsReadOnly="True" FontSize="18" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Acs ID" Binding="{Binding ACSID}"/>
                                <DataGridTextColumn Header="Acs Name" Binding="{Binding ACSName}"/>
                                <DataGridTextColumn Header="Acs Shelf" Binding="{Binding ACSShelf}"/>
                            </DataGrid.Columns>
                        </DataGrid>

                    </DockPanel>
                    <!--End GROUPBOX2's Controls-->
                </Grid>

                <!--FUNCTIONS-->
                <StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
                    <Button Content="Save" Width="100" Height="40" BorderThickness="0" Background="MediumSeaGreen" Foreground="#ffffff" FontSize="24" Margin="5"/>
                </StackPanel>
            </Grid>
        </GroupBox>
        <!--END GROUPBOX 2-->
    </Grid>
</Window>

我希望显示每个 DataGrid 的每个 ViewModel。 提前致谢!

我找到了问题的答案。感谢@Avinash Reddy 的建议。因为我的 GroupBox2 中 DataGrid 的 sub-grid 布局溢出了。它淹没了 GroupBox1 的 GridData,我没有在 GroupBox1 中看到我的 GridData 显示数据。我通过为 GroupBox2 中的 DataGrid 设置 "Height" 来修复它,并且效果很好。