c# - 如何使用 mongodb 驱动程序和绑定将 2 个集合查询到 2 个数据网格中
问题描述
我创建了一个重新排序应用程序。作为存储,我使用的是 MongoDB 服务器。我想通过单击主视图中的按钮来执行 CRUD 操作。创建操作已经顺利运行。现在我正在尝试完成 READ (QUERY) 操作。
我的 MongoDB 集合
重新订购
具有以下结构:
这里最重要的部分是“artikelliste”-key,因为它继承了我的第二个集合的所有键值对
文章
正如您在下一个结构中看到的那样(注意,对象 ID 是唯一的连接点!):
我创建了 2 个这样的数据网格:
第一个数据网格应包含我的集合 REORDERS 的值,而不包含数组artikelliste。第二个数据网格应包含我的收藏文章的值,但仅包含与相应重新排序相关的值...
这意味着如果我单击 REORDERS 数据网格的第一行,我希望第二个数据网格仅显示与此重新排序相关的文章 - 通过对象 ID 连接(因为对象 ID 是连接的唯一值这两个系列!)。
到目前为止我做了什么... XAML
<Window x:Class="Nachbestellungen.vorhandeneNachbestellungen"
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:Nachbestellungen"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="Vorhandene Nachbestellungen" WindowState="Maximized" WindowStyle="ThreeDBorderWindow">
<Window.Resources>
<Style x:Key="cellLightGray" TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="LightGray" />
</Style>
</Window.Resources>
<!-- View -->
<Grid x:Name="gridVorh">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical">
<DataGrid x:Name="dgVorh" ItemsSource="{Binding CollTop}" SelectedItem="{Binding SelItem}"
Margin="5" Grid.Row="0"
SelectionMode="Single" SelectionUnit="FullRow" IsReadOnly="False"
CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False"
BorderBrush="Black" BorderThickness="2" RowHeight="30" FontFamily="Arial Narrow" FontSize="18">
<!-- Style Column Headers -->
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Foreground" Value="#FFFFFF"/>
<Setter Property="Background" Value="#DD002C"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Height" Value="30"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Angelegt am" Binding="{Binding Angelegt_am}" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextBlock.Background" Value="LightGray"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Bearbeiter" Binding="{Binding Bearbeiter}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Bestelldatum" Binding="{Binding Bestelldatum}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Bestellt bei" Binding="{Binding Empfaenger}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Lieferung zu" Binding="{Binding Anlieferungsort}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Liefername" Binding="{Binding Adressat}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Lieferanschrift" Binding="{Binding Anschrift}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Lieferort" Binding="{Binding Plz_Ort}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Liefertermin" Binding="{Binding Liefertermin}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
</DataGrid.Columns>
</DataGrid>
<!-- ARTIKEL ZUR NACHBESTELLUNG -->
<DataGrid x:Name="dgVorhArtikel" ItemsSource="{Binding SelItem.artikelliste}"
Margin="5" Grid.Row="0"
SelectionMode="Single" SelectionUnit="Cell" IsReadOnly="False"
CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False"
BorderBrush="Black" BorderThickness="2" RowHeight="30" FontFamily="Arial Narrow" FontSize="18">
<!-- Style Column Headers -->
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Foreground" Value="#FFFFFF"/>
<Setter Property="Background" Value="#DD002C"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Height" Value="30"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Pos" Binding="{Binding Pos}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Artikelbezeichnung" Binding="{Binding Artikelbezeichnung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Artikelnummer" Binding="{Binding Artikelnummer}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Einheit" Binding="{Binding Einheit}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Menge" Binding="{Binding Menge}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Einzelpreis" Binding="{Binding Einzelpreis}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Gesamtpreis" Binding="{Binding Gesamtpreis}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Anforderungsgrund" Binding="{Binding Anforderungsgrund}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Anforderungsnr" Binding="{Binding Anforderungsnr}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Anforderer" Binding="{Binding Anforderer}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Rechnungsnr" Binding="{Binding Rechnungsnr}"/>
<DataGridTextColumn Header="AB-Nr" Binding="{Binding ABnr}"/>
<DataGridTextColumn Header="ÄndDatum" Binding="{Binding LetzteAktualisierung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Bemerkungen" Binding="{Binding Bemerkungen}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
<StackPanel Name="stpnlUpdate" Grid.Row="1" HorizontalAlignment="Center">
<Button Name="btnUpdate" Content="Aktualisieren"
Background="#DD002C" Foreground="White"
BorderThickness="2" BorderBrush="Black"
Click="BtnUpdate_Click"
Height="40" Width="130"
FontFamily="Verdana" FontStyle="Oblique"
FontStretch="ExtraCondensed" FontSize="15"
FontWeight="ExtraBlack"
MouseEnter="BtnUpdate_MouseEnter" MouseLeave="BtnUpdate_MouseLeave">
</Button>
</StackPanel>
</Grid>
代码背后
public partial class vorhandeneNachbestellungen : Window
{
public vorhandeneNachbestellungen(string hv)
{
InitializeComponent();
this.DataContext = new vorhandeneNachbestellungenViewModel(hv);
}
查看模型
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace Nachbestellungen
{
public class vorhandeneNachbestellungenViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<Nachbestellung> _collTop;
public ObservableCollection<Nachbestellung> CollTop
{
get { return _collTop; }
set
{
_collTop = value;
OnPropertyChanged("CollTop");
}
}
private ObservableCollection<Artikel> _collBot;
public ObservableCollection<Artikel> CollBot
{
get { return _collBot; }
set
{
_collBot = value;
OnPropertyChanged("CollBot");
}
}
private Nachbestellung _selItem;
public Nachbestellung SelItem
{
get { return _selItem; }
set
{
_selItem = value;
OnPropertyChanged("SelItem");
}
}
public vorhandeneNachbestellungenViewModel(string hv)
{
try
{
//connecting to database
var crud = new MongoCRUD("avdb");
//get filtered records (filtered by Id which is "Hv": hv)
var erg = crud.LoadRecords<Nachbestellung>("nachbestellungen", hv);
//filtered List is an ObservableCollection which is updated with OnPropertyChanged method
CollTop = new ObservableCollection<Nachbestellung>(erg);
//same procedure for the articles collection....
//var arterg = crud.LoadRecords<Artikel>("bestellteArtikel", hv);
CollBot = new ObservableCollection<Artikel>();
for (int i = 0; i < CollTop.Count; i++)
{
foreach (var a in CollTop[i].artikelliste)
{
CollBot.Add(a);
}
}
//wenn Abfrageergebnis null ist, dann Messagebox-Info (Zu dieser Hv wurde keine Nachbestellung gefunden!)
if (CollBot.Count <= 0)
{
MessageBox.Show("Zu dieser Hv wurde keine Nachbestellung gefunden!",
"Keine Daten vorhanden!", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}
}
解决方案
我的视图模型中的 SelItem 属性是成功的关键,因为它为您完成了所有的连接工作。您需要将其绑定到 TOP DATAGRID,然后对于 BOTTOM DATAGRID,您只需将 ITEM SOURCE 绑定到 SelValue.PropertyListOfYourModel (在我的情况下是 REORDER=Nachbestellung 中的 artikelliste[])......这样你甚至不需要不再需要 CollBot 集合了!!!...因为通过数据绑定的魔力,您的 MongoDB 已经知道哪些部分属于一起!我希望这对未来的其他开发人员也有帮助:
XAML
<Window x:Class="Nachbestellungen.vorhandeneNachbestellungen"
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:Nachbestellungen"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="Vorhandene Nachbestellungen" WindowState="Maximized" WindowStyle="ThreeDBorderWindow">
<Window.Resources>
<Style x:Key="cellLightGray" TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="LightGray" />
</Style>
</Window.Resources>
<!-- View -->
<Grid x:Name="gridVorh">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical">
<DataGrid x:Name="dgVorh" ItemsSource="{Binding CollTop}" SelectedItem="{Binding SelItem}"
Margin="5" Grid.Row="0"
SelectionMode="Single" SelectionUnit="FullRow" IsReadOnly="False"
CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False"
BorderBrush="Black" BorderThickness="2" RowHeight="30" FontFamily="Arial Narrow" FontSize="18">
<!-- Style Column Headers -->
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Foreground" Value="#FFFFFF"/>
<Setter Property="Background" Value="#DD002C"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Height" Value="30"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Angelegt am" Binding="{Binding Angelegt_am}" IsReadOnly="True">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextBlock.Background" Value="LightGray"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Bearbeiter" Binding="{Binding Bearbeiter}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Bestelldatum" Binding="{Binding Bestelldatum}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Bestellt bei" Binding="{Binding Empfaenger}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Lieferung zu" Binding="{Binding Anlieferungsort}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Liefername" Binding="{Binding Adressat}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Lieferanschrift" Binding="{Binding Anschrift}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Lieferort" Binding="{Binding Plz_Ort}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Liefertermin" Binding="{Binding Liefertermin}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
</DataGrid.Columns>
</DataGrid>
<!-- ARTIKEL ZUR NACHBESTELLUNG -->
<DataGrid x:Name="dgVorhArtikel" ItemsSource="{Binding SelItem.artikelliste}"
Margin="5" Grid.Row="0"
SelectionMode="Single" SelectionUnit="Cell" IsReadOnly="False"
CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False"
BorderBrush="Black" BorderThickness="2" RowHeight="30" FontFamily="Arial Narrow" FontSize="18">
<!-- Style Column Headers -->
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Foreground" Value="#FFFFFF"/>
<Setter Property="Background" Value="#DD002C"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="BorderThickness" Value="0,0,1,2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="FontSize" Value="18"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Height" Value="30"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Pos" Binding="{Binding Pos}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Artikelbezeichnung" Binding="{Binding Artikelbezeichnung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Artikelnummer" Binding="{Binding Artikelnummer}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Einheit" Binding="{Binding Einheit}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Menge" Binding="{Binding Menge}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Einzelpreis" Binding="{Binding Einzelpreis}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Gesamtpreis" Binding="{Binding Gesamtpreis}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Anforderungsgrund" Binding="{Binding Anforderungsgrund}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Anforderungsnr" Binding="{Binding Anforderungsnr}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Anforderer" Binding="{Binding Anforderer}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Rechnungsnr" Binding="{Binding Rechnungsnr}"/>
<DataGridTextColumn Header="AB-Nr" Binding="{Binding ABnr}"/>
<DataGridTextColumn Header="ÄndDatum" Binding="{Binding LetzteAktualisierung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
<DataGridTextColumn Header="Bemerkungen" Binding="{Binding Bemerkungen}"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
<StackPanel Name="stpnlUpdate" Grid.Row="1" HorizontalAlignment="Center">
<Button Name="btnUpdate" Content="Aktualisieren"
Background="#DD002C" Foreground="White"
BorderThickness="2" BorderBrush="Black"
Click="BtnUpdate_Click"
Height="40" Width="130"
FontFamily="Verdana" FontStyle="Oblique"
FontStretch="ExtraCondensed" FontSize="15"
FontWeight="ExtraBlack"
MouseEnter="BtnUpdate_MouseEnter" MouseLeave="BtnUpdate_MouseLeave">
</Button>
</StackPanel>
</Grid>
代码背后
public partial class vorhandeneNachbestellungen : Window
{
public vorhandeneNachbestellungen(string hv)
{
InitializeComponent();
this.DataContext = new vorhandeneNachbestellungenViewModel(hv);
}
查看模型
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace Nachbestellungen
{
public class vorhandeneNachbestellungenViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<Nachbestellung> _collTop;
public ObservableCollection<Nachbestellung> CollTop
{
get { return _collTop; }
set
{
_collTop = value;
OnPropertyChanged("CollTop");
}
}
private ObservableCollection<Artikel> _collBot;
public ObservableCollection<Artikel> CollBot
{
get { return _collBot; }
set
{
_collBot = value;
OnPropertyChanged("CollBot");
}
}
private Nachbestellung _selItem;
public Nachbestellung SelItem
{
get { return _selItem; }
set
{
_selItem = value;
OnPropertyChanged("SelItem");
}
}
public vorhandeneNachbestellungenViewModel(string hv)
{
try
{
//connecting to database
var crud = new MongoCRUD("avdb");
//get filtered records (filtered by Id which is "Hv": hv)
var erg = crud.LoadRecords<Nachbestellung>("nachbestellungen", hv);
//filtered List is an ObservableCollection which is updated with OnPropertyChanged method
CollTop = new ObservableCollection<Nachbestellung>(erg);
//same procedure for the articles collection....
//var arterg = crud.LoadRecords<Artikel>("bestellteArtikel", hv);
CollBot = new ObservableCollection<Artikel>();
for (int i = 0; i < CollTop.Count; i++)
{
foreach (var a in CollTop[i].artikelliste)
{
CollBot.Add(a);
}
}
//wenn Abfrageergebnis null ist, dann Messagebox-Info (Zu dieser Hv wurde keine Nachbestellung gefunden!)
if (CollBot.Count <= 0)
{
MessageBox.Show("Zu dieser Hv wurde keine Nachbestellung gefunden!",
"Keine Daten vorhanden!", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}
}
推荐阅读
- tensorflow - AttributeError:“list”对象没有属性“model_dir”
- javascript - 将详细信息传递给模态时 id 未定义
- animation - 容器高度动画从中间开始
- node.js - 在nodejs脚本中从shell命令运行casperjs代码时出错
- php - 如果第二个表不匹配连接条件,laravel 从第一个表中获取结果
- postman - 检索和访问字符串化数据
- wordpress - Woocommerce 登录客户 API 端点
- airflow - apache 气流 - 无法加载 dag bag 来处理故障
- mysql - MySQL 是否支持创建隐藏列?
- python - 在 shell 中将“python -m SimpleSTTPServer”的结果传递给管道