У меня есть 2 DataGrid для отображения информации из 2 таблиц, которые я получаю из базы данных MySQL. Я использую шаблон MVVM, и у меня есть 2 ViewModel под названием: "CheckJigInfoViewModel" и "AccessoryViewModel" для этих DataGrid. Я установил для этой модели ViewModel две свои DataGrid, но только один из двух DataGrid может отображать данные.
Я ошибаюсь, какой-то код? Пожалуйста, дайте мне совет!
Я прилагаю некоторое изображение для содержания выше и код для моей ViewModel:
- Код для ViewModel:
- ViewModel: CheckJigInfoViewModel
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;
}
}
}
- ViewModel: AccessoryViewModel
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;
}
}
}
- файл xaml (AccessoryInfo.xaml):
<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>
Я ожидаю показать каждую ViewModel для каждой DataGrid.
Заранее спасибо!