如何使用mongodb驱动程序和绑定将2个集合查询为2个数据网格

我已经创建了一个重新订购应用程序。作为存储,我使用的是MongoDB服务器。 我想通过单击主视图中的按钮来执行CRUD操作。创建操作已经顺利运行。 现在,我正在尝试完成READ(查询)操作。

我的MongoDB集合

  

重新订购

具有以下结构:

如何使用mongodb驱动程序和绑定将2个集合查询为2个数据网格

这里最重要的部分是“ artikelliste” 键,因为它继承了我的第二个集合的所有键值对

  

文章

如您在下一个结构中看到的(请注意,对象ID是唯一的连接点!)

如何使用mongodb驱动程序和绑定将2个集合查询为2个数据网格

我创建了2个这样的数据网格:

如何使用mongodb驱动程序和绑定将2个集合查询为2个数据网格

第一个数据网格应包含我的集合REORDERS的值,而不包含数组 artikelliste 。 第二个数据网格应包含我的集合ARTICLES的值,但仅包含与相应重新排序相关的值...

这是什么意思,如果我单击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,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,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);
            }
        }           
    }
}
handanlove521 回答:如何使用mongodb驱动程序和绑定将2个集合查询为2个数据网格

我的视图模型中的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,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,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);
            }
        }           
    }
}
本文链接:https://www.f2er.com/3125154.html

大家都在问