C# 如何在单击按钮后使用ICommand执行多个方法
我不了解如何使用ICommand inferface在一行中执行多个方法。理想情况下,我希望创建一个委托,通过调用它来执行以下4个方法中的所有方法(但它不必是委托):C# 如何在单击按钮后使用ICommand执行多个方法,c#,wpf,design-patterns,icommand,C#,Wpf,Design Patterns,Icommand,我不了解如何使用ICommand inferface在一行中执行多个方法。理想情况下,我希望创建一个委托,通过调用它来执行以下4个方法中的所有方法(但它不必是委托): var mongo=DbConn()//连接到MongoDB var artikel=GetArtikel(再订单视图)//返回ObservableCollection 这是视图设计中的数据网格(虚拟示例) 绿色按钮是我希望在ICommand界面的帮助下执行的插入部分,即单击后执行以下操作: 连接到MongoDB 把可观察到的集
var mongo=DbConn()//连接到MongoDB
var artikel=GetArtikel(再订单视图)//返回ObservableCollection
这是视图设计中的数据网格(虚拟示例)
绿色按钮是我希望在ICommand界面的帮助下执行的插入部分,即单击后执行以下操作:
连接到MongoDB
把可观察到的集合给我
给我ReorderModel实例
在我的数据库集合中插入
XAML:
<Button Name="btnSave" Margin="3,0,3,0" Content="Save Reorder"
BorderThickness="0" BorderBrush="Green"
Height="30" FontWeight="Bold" FontSize="15"
Background="Green"
VerticalAlignment="Top"
Command="{Binding SaveNb}">
</Button>
public partial class addReorder : System.Windows.Window
{
ReorderViewModel nachbest;
ArtikelViewModel artikel;
//IControllerNachbestellung controllerNachbestellung;
//IModelNachbestellung modelNachbestellung;
public ObservableCollection<string> AnfCollection { get; set; }
public addReorder()
{
this.DataContext = new addReorderViewModel();
}
public addReorder(string hv)
{
InitializeComponent();
//HV, BV, Projektleiter und Bauleiter holen
var dbOracle = new Datenbank();
txtBv.Text = dbOracle.GetBauvorhaben(hv);
txtbHv.Text = hv;
txtBauleiter.Text = dbOracle.GetBauleiter(hv);
string pl = dbOracle.GetProjektleiter(hv);
txtProjektleiter.Text = dbOracle.GetProjektleiter(hv);
nachbest = new ReorderViewModel();
artikel = new ArtikelViewModel();
leftStPnl.DataContext = nachbest;
rightStPnl.DataContext = nachbest;
dgArtikel.DataContext = artikel;
//Die Anforderungscollection wird befüllt
IAnforderungsgrund anfgruende = new Anforderungsgrund();
AnfCollection = anfgruende.ListeAnforderungen();
dgcbAnf.ItemsSource = AnfCollection;
dgcbAnf.TextBinding = new Binding(AnfCollection.ToString());
}
}
public class addReorderViewModel
{
//Hook to class DelegateCommand
private readonly DelegateCommand<Button> _clickCommand;
private ICommand saveNb { get; set; }
public addReorderViewModel(addReorderView reorderview)
{
}
public ICommand SaveNb
{
get
{
return saveNb;
}
set
{
saveNb = value;
}
}
public MongoCRUD DbConn()
{
//Connection to MongoDB database "avdb"
var mongo = new MongoCRUD("avdb");
return mongo; //return value needed for INSERT-METHOD!
}
public ObservableCollection<Artikel> GetArtikel()
{
//How many rows inside the datagrid dgArtikel?
int countrows = view.dgArtikel.Items.Count;
//How many Columns?
int countcols = view.dgArtikel.Columns.Count;
//Arrays
DataGridRow[] row = new DataGridRow[countrows];
DataGridCell[] RowColumn = new DataGridCell[countcols];
string[] CellValue = new string[countcols];
string[,] ds = new string[countrows, countcols];
ObservableCollection<Artikel> artikel = new ObservableCollection<Artikel>() { };
//Save all cell values inside multidimensional Array
for (int i = 0; i < countrows; i++)
{
//Create object for each DataGridRow of dgArtikel
row[i] = (DataGridRow)view.dgArtikel.ItemContainerGenerator.ContainerFromIndex(i);
//Alle Spalten in der jeweiligen Zeile iterieren
for (int j = 0; j < view.dgArtikel.Columns.Count; j++)
{
RowColumn[j] = view.dgArtikel.Columns[j].GetCellContent(row[i]).Parent as DataGridCell;
//ATTENTION HARDCODING --> j==INDEX COMBOBOXCOLUMN!!!
if (j == 7) //<---!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
CellValue[j] = ((ComboBox)RowColumn[j].Content).Text;
}
else
{
CellValue[j] = ((TextBlock)RowColumn[j].Content).Text;
}
ds[i, j] = CellValue[j];
}
//The sequence must be the same as inside class ARTIKEL! ATTENTION HARDCODING!
var art = new Artikel
{
Pos = ds[i, 0],
Artikelbezeichnung = ds[i, 1],
Artikelnummer = ds[i, 2],
Einheit = ds[i, 3],
Menge = ds[i, 4],
Einzelpreis = ds[i, 5],
Gesamtpreis = ds[i, 6],
Anforderungsgrund = ds[i, 7], //<---!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Anforderungsnr = ds[i, 8],
Anforderer = ds[i, 9],
Bemerkungen = ds[i, 10]
};
if (art != null)
artikel.Add(art);
}
return artikel;
}
public ReorderModel GetReorder(ObservableCollection<Artikel> artikel, addReorderView reorderview)
{
ReorderModel rom = new ReorderModel
{
Hv = view.txtbHv.Text,
Bv = view.txtBv.Text,
Bauleiter = view.txtBauleiter.Text,
Empfaenger = view.cboxEmpfaenger.Text,
Empf_Ansprechpartner = view.txtEmpfAnsprechpartner.Text,
Empfaenger_Mail = view.txtEmpfMail.Text,
Anlieferungsort = view.cboxAnlieferung.Text,
Adressat = view.cboxAdressat.Text,
Anschrift = view.txtAdresse.Text,
Plz_Ort = view.txtPlzOrt.Text,
Kontaktperson_Kontaktnr = view.txtAnsprechpartnerOrt.Text,
Liefertermin = view.calLiefertermin.Text,
Bearbeiter = view.cboxBearbeiter.Text,
Telefon_Bearbeiter = view.txtBearbeiterTel.Text,
Mail_Bearbeiter = view.txtBearbeiterMail.Text,
Bestelldatum = view.calBestelldatum.Text,
Bemerkung_oben = view.txtBemerkung.Text,
Projektleiter = view.txtProjektleiter.Text,
Angelegt_am = DateTime.Now.ToString(),
artikelliste = artikel
};
return rom;
}
public void Insert(MongoCRUD mongo, ObservableCollection<Artikel> artikel, ReorderModel rom)
{
foreach (var a in artikel)
{
mongo.InsertRecord<Artikel>("bestellteArtikel", a);
}
mongo.InsertRecord<Nachbestellung>("nachbestellungen", nb);
MessageBox.Show("Nachbestellung in Datenbank gespeichert!", "Erfolgreich!", MessageBoxButton.OK, MessageBoxImage.Information);
}
<Window x:Class="Nachbestellungen.neueNachbestellung"
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"
FontFamily="Arial Narrow"
Title="Nachbestellungen" Height="900" Width="900" FontWeight="Bold">
<Window.Resources>
<!-- Abstand zwischen Textblock und Textbox automatisch setzen -->
<Style TargetType="TextBox">
<Setter Property="Margin" Value="0,0,0,0"/>
</Style>
</Window.Resources>
<Grid Name="uInputGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- links -->
<StackPanel Grid.Column="0" Margin="25,25,0,0" x:Name="leftStPnl">
<StackPanel x:Name="pnlHeader" Grid.Column="0" Grid.Row="0" Orientation="Vertical">
<TextBlock Name="tbNb" Grid.Row="0" Grid.Column="0" Text="NACHBESTELLUNG" FontWeight="Bold" FontSize="20"/>
</StackPanel>
<StackPanel x:Name="pnlBv" Grid.Column="0" Grid.Row="1" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBv" Text="Bauvorhaben" DockPanel.Dock="Left" FontSize="14" FontWeight="Bold"/>
<TextBox Name="txtBv" FontSize="12" DockPanel.Dock="Right" IsEnabled="False" Width="150" Margin="68,0,0,0"></TextBox>
</StackPanel>
<StackPanel x:Name="pnlHv" Grid.Column="0" Grid.Row="2" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbHv" Grid.Column="0" Text="HV-Nummer" FontSize="14" FontWeight="Bold"/>
<TextBox x:Name="txtbHv" FontSize="12" IsEnabled="False" Width="60" Margin="72,0,0,0"/>
</StackPanel>
<StackPanel x:Name="pnlBauleiter" Grid.Column="0" Grid.Row="3" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBauleiter" Text="Bauleiter" FontSize="14" FontWeight="Bold"/>
<TextBox x:Name="txtBauleiter" FontSize="12" IsEnabled="False" Width="150" Margin="90,0,0,0"/>
</StackPanel>
<StackPanel x:Name="pnlGap1" Grid.Column="0" Grid.Row="4" Margin="0,10,0,0" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="pnlEmpfaenger" Grid.Column="0" Grid.Row="5" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbEmpfaenger" Text="AN" FontSize="14" FontWeight="Bold"/>
<ComboBox x:Name="cboxEmpfaenger" FontSize="12" Width="150" Margin="118,0,0,0" Loaded="ComboBox_Loaded" SelectionChanged="ComboBox_Changed">
</ComboBox>
</StackPanel>
<StackPanel x:Name="pnlEmpfAnsprechpartner" Grid.Column="0" Grid.Row="6" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbEmpfAnsprechpartner" Text="Ansprechpartner" FontSize="14" FontWeight="Bold"/>
<TextBox x:Name="txtEmpfAnsprechpartner" FontSize="12" IsEnabled="False" Width="150" Margin="50,0,0,0"/>
</StackPanel>
<StackPanel x:Name="pnlEmpfMail" Grid.Column="0" Grid.Row="7" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbEmpfMail" Text="Mailadresse" FontSize="14" FontWeight="Bold"/>
<TextBox x:Name="txtEmpfMail" FontSize="12" IsEnabled="False" Width="150" Margin="73,0,0,0"/>
</StackPanel>
<StackPanel x:Name="pnlGap2" Grid.Column="0" Grid.Row="8" Margin="0,10,0,0" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="pnlAnlieferung" Grid.Column="0" Grid.Row="9" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbAnlieferung" Text="Anlieferung" FontSize="14" FontWeight="Bold"/>
<ComboBox x:Name="cboxAnlieferung" FontSize="12" Width="150" Margin="77,0,0,0" Loaded="ComboBoxAnlieferung_Loaded" SelectionChanged="ComboBoxAnlieferung_Changed"/>
</StackPanel>
<StackPanel x:Name="pnlBauherr" Grid.Column="0" Grid.Row="10" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBauherr" FontSize="14" FontWeight="Bold"/>
<ComboBox x:Name="cboxAdressat" FontSize="12" IsEnabled="False" Width="200" Margin="135,0,0,0" DropDownClosed="CboxAdressat_DropDownClosed"/>
</StackPanel>
<StackPanel x:Name="pnlAdresse" Grid.Column="0" Grid.Row="11" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbAdresse" FontSize="14" FontWeight="Bold" Text="Adresse"/>
<TextBox x:Name="txtAdresse" FontSize="12" IsEnabled="False" Width="150" Margin="93,0,0,0"/>
</StackPanel>
<StackPanel x:Name="pnlPlzOrt" Grid.Column="0" Grid.Row="12" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbPlzOrt" FontSize="14" FontWeight="Bold"/>
<TextBox x:Name="txtPlzOrt" FontSize="12" IsEnabled="False" Width="150" Margin="135,0,0,0"/>
</StackPanel>
<StackPanel x:Name="pnlGap3" Grid.Column="0" Grid.Row="13" Margin="0,10,0,0" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="pnlAnsprechpartnerOrt" Grid.Column="0" Grid.Row="14" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbAnsprechpartnerOrt" FontSize="14" FontWeight="Bold" Text="Ansprechpartner vor Ort/Telefon"/>
<TextBox x:Name="txtAnsprechpartnerOrt" FontSize="12" Width="190" Margin="50,0,0,0" AcceptsReturn="False"/>
<Button x:Name="btnKontakt" Content="Kontakt auswählen" FontSize="12" Click="btnKontakt_Click" />
</StackPanel>
<StackPanel x:Name="pnlBemerkung" Grid.Column="0" Grid.Row="14" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBemerkung" FontSize="14" FontWeight="Bold" Text="Bemerkung"/>
<TextBox x:Name="txtBemerkung" FontSize="12" Width="250" Height="40" Margin="76,0,0,0" AcceptsReturn="False"/>
</StackPanel>
<StackPanel x:Name="pnlLiefertermin" Grid.Column="0" Grid.Row="15" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbLiefertermin" FontSize="14" FontWeight="Bold" Text="Liefertermin"/>
<DatePicker x:Name="calLiefertermin" FontSize="12" Width="160" Margin="75,0,0,0" IsTodayHighlighted="true"/>
</StackPanel>
</StackPanel>
<!--rechts-->
<StackPanel Grid.Column="1" Margin="0,25,25,0" x:Name="rightStPnl">
<StackPanel x:Name="pnlLuxLogo" Grid.Column="1" Grid.Row="0" Margin="0,10,0,0" Orientation="Vertical">
<Image Source="Z:\SchaeferT\Projekte\Haustechnik\Nachbestellungen\Nachbestellungen\LuxLogo.png" Width="200" Height="30" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel x:Name="pnlFirma" Grid.Column="1" Grid.Row="1" Margin="0,27,0,0" Orientation="Vertical">
<TextBlock x:Name="tbFirma" FontSize="12" Text="Lux Projektmanagement GmbH & Co. KG"/>
</StackPanel>
<StackPanel x:Name="pnlFirmaAnschrift" Grid.Column="1" Grid.Row="2" Margin="0,10,0,0" Orientation="Vertical">
<TextBlock x:Name="tbFirmaAnschrift" FontSize="12" Text="Pleinfelder Straße 64"/>
</StackPanel>
<StackPanel x:Name="pnlFirmaPlzOrt" Grid.Column="1" Grid.Row="3" Margin="0,10,0,0" Orientation="Vertical">
<TextBlock x:Name="tbFirmaPlzOrt" FontSize="12" Text="91166 Georgensgmünd"/>
</StackPanel>
<StackPanel x:Name="pnlRightGap1" Grid.Column="1" Grid.Row="4" Margin="0,10,0,0" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="pnlBearbeiter" Grid.Column="1" Grid.Row="5" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBearbeiter" FontSize="14" FontWeight="Bold" Text="Bearbeiter"/>
<ComboBox x:Name="cboxBearbeiter" FontSize="12" Width="150" Margin="30,0,0,0" Loaded="ComboBoxBearbeiter_Loaded" SelectionChanged="ComboBoxBearbeiter_Changed"/>
</StackPanel>
<StackPanel x:Name="pnlBearbeiterTel" Grid.Column="0" Grid.Row="6" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBearbeiterTel" FontSize="14" FontWeight="Bold" Text="Telefon"/>
<TextBox x:Name="txtBearbeiterTel" FontSize="12" IsEnabled="False" Width="150" Margin="45,0,0,0"/>
</StackPanel>
<StackPanel x:Name="pnlBearbeiterMail" Grid.Column="0" Grid.Row="7" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBearbeiterMail" FontSize="14" FontWeight="Bold" Text="Mail"/>
<TextBox x:Name="txtBearbeiterMail" FontSize="12" IsEnabled="False" Width="150" Margin="62,0,0,0"/>
</StackPanel>
<StackPanel x:Name="pnlBestelldatum" Grid.Column="0" Grid.Row="8" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBestelldatum" FontSize="14" FontWeight="Bold" Text="Best.-Datum"/>
<DatePicker x:Name="calBestelldatum" FontSize="12" Width="200" Margin="19,0,0,0" IsTodayHighlighted="true"/>
</StackPanel>
<StackPanel x:Name="pnlRightGap2" Grid.Column="1" Grid.Row="9" Margin="0,10,0,0" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="pnlRightGap3" Grid.Column="1" Grid.Row="10" Margin="0,10,0,0" Orientation="Horizontal">
</StackPanel>
<StackPanel x:Name="pnlHinweis" Grid.Column="1" Grid.Row="11" Orientation="Vertical">
<TextBlock x:Name="tbHinweis" FontSize="12">
<Run Text=" Bitte geben sie auf ihren Dokumenten immer folgende Informationen an:
• Name des Bauvorhabens
• HV-Nummer und/oder Kostenstelle
• Name des Bestellers
• Abholer (nur bei Abholungen)
Entsprechende Angaben sind in dieser Bestellung fett/kursiv markiert. "/>
<Run FontFamily="Verdana" FontSize="11" Text="Rechnungen mit unvollständigen Angaben müssen wir ab dem 01.12.2013 ungebucht an sie zurücksenden. "/>
</TextBlock>
</StackPanel>
<StackPanel x:Name="pnlProjektleiter" Grid.Column="0" Grid.Row="14" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbProjektleiter" FontSize="14" FontWeight="Bold" Text="Projektleiter"/>
<TextBox x:Name="txtProjektleiter" Text="{Binding Projektleiter, Mode=OneWay}" FontSize="12" IsEnabled="False" Width="225" Margin="50,0,0,0" AcceptsReturn="False"/>
</StackPanel>
</StackPanel>
<!-- unten -->
<DataGrid x:Name="dgArtikel" ItemsSource="{Binding Artikel}" Margin="5" Grid.Row="1" Grid.ColumnSpan="2"
SelectionMode="Single" SelectionUnit="Cell" IsReadOnly="False"
CanUserAddRows="True" CanUserDeleteRows="True" AutoGenerateColumns="False"
BorderBrush="Black" BorderThickness="2" RowHeight="30" FontFamily="Arial Narrow" FontSize="18"
LostFocus="DgArtikel_LostFocus" GotFocus="DgArtikel_GotFocus">
<!-- 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}"/>
<DataGridTextColumn Header="Artikelbezeichnung" Binding="{Binding Artikelbezeichnung}"/>
<DataGridTextColumn Header="Artikelnummer" Binding="{Binding Artikelnummer}"/>
<DataGridTextColumn Header="Einheit" Binding="{Binding Einheit}"/>
<DataGridTextColumn Header="Menge" Binding="{Binding Menge}"/>
<DataGridTextColumn Header="Einzelpreis" Binding="{Binding Einzelpreis}"/>
<DataGridTextColumn Header="Gesamtpreis" Binding="{Binding Gesamtpreis}"/>
<DataGridComboBoxColumn Header="Anforderungsgrund" x:Name="dgcbAnf" ItemsSource="{Binding Anforderungsgrund}"/>
<DataGridTextColumn Header="Anforderungsnr" Binding="{Binding Anforderungsnr}"/>
<DataGridTextColumn Header="Anforderer" Binding="{Binding Anforderer}"/>
<DataGridTextColumn Header="Bemerkungen" Binding="{Binding Bemerkungen}"/>
</DataGrid.Columns>
</DataGrid>
<Border x:Name="borderBtnArtikel" Grid.Row="2" Grid.Column="0" Margin="25,0,0,0" CornerRadius="10" BorderBrush="Black" BorderThickness="2" Background="PaleVioletRed" HorizontalAlignment="Left">
<Button Name="btnArtikel" Margin="3,0,3,0" Content="Artikel suchen" Height="30" FontWeight="Bold" FontSize="15" Background="PaleVioletRed" Click="BtnArtikel_Click" VerticalAlignment="Top" BorderThickness="0" BorderBrush="PaleVioletRed"></Button>
</Border>
<Border x:Name="borderBtnVorhanden" Grid.Row="2" Grid.Column="0" Margin="0,0,25,0" CornerRadius="10" BorderBrush="Black" BorderThickness="2" Background="Yellow" HorizontalAlignment="Right">
<Button Name="btnVorhanden" Margin="3,0,3,0" Content="Vorhandene Nb Aufrufen" Height="30" FontWeight="Bold" FontSize="15" Background="Yellow" Click="BtnVorhanden_Click" VerticalAlignment="Top" BorderThickness="0" BorderBrush="Yellow"></Button>
</Border>
<Border Grid.Row="2" Grid.Column="1" CornerRadius="10" BorderBrush="Black" BorderThickness="2" Background="Green" Margin="25,0,0,0" HorizontalAlignment="Left">
<Button Name="btnSpeichern" Margin="3,0,3,0" Content="Neue Nb Speichern"
BorderThickness="0" BorderBrush="Green"
Height="30" FontWeight="Bold" FontSize="15"
Background="Green"
VerticalAlignment="Top"
Command="{Binding SaveNb}"
>
</Button>
<!-- Click="BtnSpeichern_Click" -->
</Border>
<Border Grid.Row="2" Grid.Column="1" CornerRadius="10" BorderBrush="Black" BorderThickness="2" Background="Orange" HorizontalAlignment="Right" Margin="0,0,25,0">
<Button Name="btnOutlook" Margin="3,0,3,0" Content="Nb per Outlook versenden" Height="30" BorderThickness="0" BorderBrush="Orange" FontWeight="Bold" FontSize="15" Background="Orange" Click="BtnOutlook_Click"></Button>
</Border>
</Grid>
如您所见,Outlook操作(橙色按钮)仍然与CodeBehind中的BtnOutlook_Click事件处理程序紧密耦合,设计非常糟糕。因此,在将绿色SaveNb按钮正确绑定到上述4种方法之后,我将继续使用此按钮和其他按钮,根据标准约定将它们逐个与CodeBehind分离,以便更好地重用和灵活地编写代码。WPF中的命令是在从UI生成命令时执行一种方法(单击按钮)。现在您有4个方法要在一个命令上执行。因此,创建一个包含四个方法调用的方法(或操作),并将该方法绑定到命令
在视图模型的构造函数中添加如下内容
public addReorderViewModel()
{
saveNb = new DelegateCommand<Button>(() =>{
var mongo = DbConn(); //Connection to MongoDB
var artikel = GetArtikel(reorderView); //returns ObservableCollection<ArtikelModel>
var reorder = GetReorder(artikel, reorderView); //returns ReorderModel
Insert(mongo, artikel, reorder); //inserts new reorder into the MongoDB database (one reorder has multiple articles(artikel))
});
}
public addReorderViewModel()
{
saveNb=新的DelegateCommand(()=>{
var mongo=DbConn();//到MongoDB的连接
var artikel=GetArtikel(reorderView);//返回ObservableCollection
var reorder=GetReorder(artikel,reorderView);//返回ReorderModel
插入(mongo,artikel,reorder);//将新的重新排序插入MongoDB数据库(一个重新排序有多个项目(artikel))
});
}
由于您使用的是DelegateCommand
,因此只需正确初始化即可:
AddReorderViewModel.cs
public class DelegateCommand<T> : System.Windows.Input.ICommand
{
private readonly Predicate<T> _canExecute;
private readonly Action<T> _execute;
public DelegateCommand(Action<T> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<T> execute, Predicate<T> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
return true;
return _canExecute((parameter == null) ? default(T) : (T)Convert.ChangeType(parameter, typeof(T)));
}
public void Execute(object parameter)
{
_execute((parameter == null) ? default(T) : (T)Convert.ChangeType(parameter, typeof(T)));
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
public ICommand SaveNb => new DelegateCommand<object>(ExecuteMethods, CanExecuteMethods);
private void ExecuteMethods(object param)
{
var mongo = DbConn(); //Connection to MongoDB
var artikel = GetArtikel(reorderView); //returns ObservableCollection<ArtikelModel>
var reorder = GetReorder(artikel, reorderView); //returns ReorderModel
Insert(mongo, artikel, reorder); //inserts new reorder into the MongoDB database (one reorder has multiple articles(artikel))
}
private bool CanExecuteMethods(object param)
{
// Check if command can execute
if (MongoDb connection exists)
{
return true;
}
return false;
}
视图模型:
public class addReorderViewModel
{
public addReorderViewModel(string hvId)
{
InitializeReorderModel(hvId);
this.ArtikelList = new ObservableCollection<Artikel>();
//Die Anforderungscollection wird befüllt
IAnforderungsgrund anfgruende = new Anforderungsgrund();
this.AnfCollection = anfgruende.ListeAnforderungen();
}
public InitializeReorderModel(string hvId)
{
this.Nachbest = new ReorderModel();
//HV, BV, Projektleiter und Bauleiter holen
var dbOracle = new Datenbank();
this.Nachbest.BV = dbOracle.GetBauvorhaben(hv);
this.Nachbest.Hv = hv;
this.Nachbest.Bauleiter = dbOracle.GetBauleiter(hv);
this.Nachbest.Projektleiter = dbOracle.GetProjektleiter(hv);
}
private void ExecuteMethods(object param)
{
var mongo = DbConn(); //Connection to MongoDB
var artikel = this.ArtikelList; //returns ObservableCollection<ArtikelModel>
var reorder = this.Nachbest; //returns ReorderModel
Insert(mongo, artikel, reorder); //inserts new reorder into the MongoDB database (one reorder has multiple articles(artikel))
}
public ICommand SaveNb => new DelegateCommand<object>(ExecuteMethods, CanExecuteMethods);
public ReorderViewModel Nachbest {get; set;}
public ObservableCollection<string> AnfCollection {get; set;}
public ObservableCollection<Artikel> ArtikelList {get; set;}
}
public类addReorderViewModel
{
公共addReorderViewModel(字符串hvId)
{
初始化医嘱模型(hvId);
this.artikelist=新的可观察集合();
//在未完成之前,请按顺序进行收集
IAnforderungsgrund ANFGRUNDE=新的Anforderungsgrund();
this.AnfCollection=anfgruende.listenfordungen();
}
公共初始值设定项OrderModel(字符串hvId)
{
this.Nachbest=new reodermodel();
//HV、BV、Projektleiter和Bauleiter holen
var dbOracle=new Datenbank();
this.Nachbest.BV=dbOracle.GetBauvorhaben(hv);
this.Nachbest.Hv=Hv;
this.Nachbest.Bauleiter=dbOracle.GetBauleiter(hv);
this.Nachbest.Projektleiter=dbOracle.GetProjektleiter(hv);
}
私有void ExecuteMethods(对象参数)
{
var mongo=DbConn();//到MongoDB的连接
var artikel=this.artikelist;//返回ObservableCollection
var reorder=this.Nachbest;//返回ReorderModel
插入(mongo,artikel,reorder);//将新的重新排序插入MongoDB数据库(一个重新排序有多个项目(artikel))
}
public ICommand SaveNb=>newdelegateCommand(ExecuteMethods、CanExecuteMethods);
公共ReorderViewModel Nachbest{get;set;}
public observateCollection AnfCollection{get;set;}
公共可观测集合炮兵{get;set;}
}
XAML代码段:
<!-- links -->
<StackPanel x:Name="leftStPnl" DataContext="{Binding Nachbest}" ... >
<StackPanel x:Name="pnlBv" Grid.Column="0" Grid.Row="1" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBv" Text="Bauvorhaben" DockPanel.Dock="Left" FontSize="14" FontWeight="Bold"/>
<TextBox Name="txtBv" Text="{Binding Bv }" />
</StackPanel>
<StackPanel x:Name="pnlHv" Grid.Column="0" Grid.Row="2" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbHv" Grid.Column="0" Text="HV-Nummer" FontSize="14" FontWeight="Bold"/>
<TextBox x:Name="txtbHv" Text="{Binding Hv}"/>
</StackPanel>
...
</StackPanel>
<!-- rechts -->
<StackPanel x:Name="rightStPnl" DataContext="{Binding Nachbest}" ... >
...
</StackPanel>
<DataGrid x:Name="dgArtikel" ItemsSource="{Binding ArtikelList}" ... >
...
</DataGrid>
...
...
...
我注意到您的ViewModel没有实现INotifyPropertyChanged
-这是故意的吗?您使用的是MVVM框架吗?如果你不这样做,那么你将不得不做大量的腿部工作和重复编码。这不是MVVM的实现方式。我认为您误解了MVVM的工作原理
public class addReorderViewModel
{
public addReorderViewModel(string hvId)
{
InitializeReorderModel(hvId);
this.ArtikelList = new ObservableCollection<Artikel>();
//Die Anforderungscollection wird befüllt
IAnforderungsgrund anfgruende = new Anforderungsgrund();
this.AnfCollection = anfgruende.ListeAnforderungen();
}
public InitializeReorderModel(string hvId)
{
this.Nachbest = new ReorderModel();
//HV, BV, Projektleiter und Bauleiter holen
var dbOracle = new Datenbank();
this.Nachbest.BV = dbOracle.GetBauvorhaben(hv);
this.Nachbest.Hv = hv;
this.Nachbest.Bauleiter = dbOracle.GetBauleiter(hv);
this.Nachbest.Projektleiter = dbOracle.GetProjektleiter(hv);
}
private void ExecuteMethods(object param)
{
var mongo = DbConn(); //Connection to MongoDB
var artikel = this.ArtikelList; //returns ObservableCollection<ArtikelModel>
var reorder = this.Nachbest; //returns ReorderModel
Insert(mongo, artikel, reorder); //inserts new reorder into the MongoDB database (one reorder has multiple articles(artikel))
}
public ICommand SaveNb => new DelegateCommand<object>(ExecuteMethods, CanExecuteMethods);
public ReorderViewModel Nachbest {get; set;}
public ObservableCollection<string> AnfCollection {get; set;}
public ObservableCollection<Artikel> ArtikelList {get; set;}
}
<!-- links -->
<StackPanel x:Name="leftStPnl" DataContext="{Binding Nachbest}" ... >
<StackPanel x:Name="pnlBv" Grid.Column="0" Grid.Row="1" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbBv" Text="Bauvorhaben" DockPanel.Dock="Left" FontSize="14" FontWeight="Bold"/>
<TextBox Name="txtBv" Text="{Binding Bv }" />
</StackPanel>
<StackPanel x:Name="pnlHv" Grid.Column="0" Grid.Row="2" Margin="0,10,0,0" Orientation="Horizontal">
<TextBlock Name="tbHv" Grid.Column="0" Text="HV-Nummer" FontSize="14" FontWeight="Bold"/>
<TextBox x:Name="txtbHv" Text="{Binding Hv}"/>
</StackPanel>
...
</StackPanel>
<!-- rechts -->
<StackPanel x:Name="rightStPnl" DataContext="{Binding Nachbest}" ... >
...
</StackPanel>
<DataGrid x:Name="dgArtikel" ItemsSource="{Binding ArtikelList}" ... >
...
</DataGrid>