Ok,
Samotný kód aplikace:
namespace Dodavatele
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class DodavatelInfo : Window
{
private NorthwindDataContext ndc = null; //Objet DataContext
private Supplier dodavatel = null; //Entitní třída tabulky dodavatel
private BindingList<Product> vyrobkyInfo = null;
public DodavatelInfo()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ndc = new NorthwindDataContext();
this.seznamDoDodavatelu.DataContext = ndc.Suppliers;
}
private void seznamDoDodavatelu_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
dodavatel = this.seznamDoDodavatelu.SelectedItem as Supplier;
IList seznam = ((IListSource)dodavatel.Products).GetList();
vyrobkyInfo = seznam as BindingList<Product>;
this.seznamVyrobku.DataContext = vyrobkyInfo;
}
private void seznamVyrobku_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Enter:
upravVyrobek(this.seznamVyrobku.SelectedItem as Product);
break;
case Key.Insert:
pridejVyrobek();
break;
case Key.Delete:
vymazVyrobek(this.seznamVyrobku.SelectedItem as Product);
break;
}
}
private void pridejVyrobek()
{
ForularVyrobku fv = new ForularVyrobku(); Vytvořím přidávací okno
fv.Title = "Nový výrobek pro " + dodavatel.CompanyName;
if (fv.ShowDialog().Value)
{
Product novyVyrobek = new Product();
novyVyrobek.ProductName = fv.nazevVyrobku.Text;
novyVyrobek.UnitPrice = Decimal.Parse(fv.cenaZaJednotku.Text);
novyVyrobek.QuantityPerUnit = fv.mnozstevniJednotka.Text;
novyVyrobek.SupplierID = dodavatel.SupplierID; //Pokud tento řádek dodám za vyrobkyInfo.Add... tak vše funguje, tímto naplním vlastnost, která slouží k svázání s tabulkou supliers a to očividné vadí třídě BindingList<T>.
vyrobkyInfo.Add(novyVyrobek);
dodavatel.Products.Add(novyVyrobek); //Přidám do kolekce entitních tříd, která se poté eventuelně aktualizuje do databáze
this.ulozitZmeny.IsEnabled = true;
}
}
private void vymazVyrobek(Product product)
{
MessageBoxResult odpoved = MessageBox.Show("Vymazat " + product.ProductName, "Potvrzení", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (odpoved == MessageBoxResult.Yes)
{
dodavatel.Products.Remove(product);
vyrobkyInfo.Remove(product);
this.ulozitZmeny.IsEnabled = true;
}
}
private void upravVyrobek(Product product)
{
ForularVyrobku fv = new ForularVyrobku();
fv.Title = "Úprava údajů o výrobku";
fv.nazevVyrobku.Text = product.ProductName;
fv.mnozstevniJednotka.Text = product.QuantityPerUnit;
fv.cenaZaJednotku.Text = product.UnitPrice.ToString();
if (fv.ShowDialog().Value)
{
product.ProductName = fv.nazevVyrobku.Text;
product.QuantityPerUnit = fv.mnozstevniJednotka.Text;
product.UnitPrice = decimal.Parse(fv.cenaZaJednotku.Text);
this.ulozitZmeny.IsEnabled = true;
}
}
private void ulozitZmeny_Click(object sender, RoutedEventArgs e)
{
try
{
ndc.SubmitChanges();
ulozitZmeny.IsEnabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Chyba při ukládání změn");
}
}
private void cancel_Click(object sender, RoutedEventArgs e) //BlaBlaBla
{
Window_Loaded(new object(), new RoutedEventArgs());
// ndc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, ndc.Suppliers);
}
}
}
Kdyby mél někdo potřebu kouknout do kompletních zdrojáků, tak tady je XAML obou okonen.
XAML hlavního okna
<Window x:Class="Dodavatele.DodavatelInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:Dodavatele"
Title="Informace o dodavateli" Height="350" Width="525"
Loaded="Window_Loaded">
<Window.Resources>
<app:PrevodnikCeny x:Key="prevodnikMeny" />
<DataTemplate x:Key="dodaveteleSablona">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=SuplierID}" />
<TextBlock Text=" : " />
<TextBlock Text="{Binding Path=SuplierName}" />
<TextBlock Text=" : " />
<TextBlock Text="{Binding Path=ContactName}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource dodaveteleSablona}" Height="21" HorizontalAlignment="Stretch" Margin="40,16,42,0" Name="seznamDoDodavatelu" VerticalAlignment="Top" Width="Auto" SelectionChanged="seznamDoDodavatelu_SelectionChanged" />
<Button Content="Uložit změny" Height="23" HorizontalAlignment="Left" Margin="40,0,0,10" Name="ulozitZmeny" VerticalAlignment="Bottom" Width="90" Click="ulozitZmeny_Click" />
<ListView IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" Height="Auto" HorizontalAlignment="Stretch" Margin="40,44,40,60" Name="seznamVyrobku" VerticalAlignment="Stretch" Width="Auto" KeyDown="seznamVyrobku_KeyDown" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="75" Header="ID výrobku" DisplayMemberBinding="{Binding Path=ProductID}" />
<GridViewColumn Width="225" Header="Název" DisplayMemberBinding="{Binding Path=ProductName}" />
<GridViewColumn Width="135" Header="Množstevní jednotka" DisplayMemberBinding="{Binding Path=QuantityPerUnit, Converter={StaticResource prevodnikMeny}}" />
<GridViewColumn Width="75" Header="Jednotková cena" DisplayMemberBinding="{Binding Path=UnitPrice}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<Button Content="Cancel" Height="23" HorizontalAlignment="Right" Margin="0,0,42,12" Name="cancel" VerticalAlignment="Bottom" Width="103" Click="cancel_Click" />
</Grid>
</Window>
XAML přidávacího okna:
<Window x:Class="Dodavatele.ForularVyrobku"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ForularVyrobku" Height="225" Width="515" ResizeMode="NoResize">
<Grid>
<Button Content="OK" Height="23" HorizontalAlignment="Left" Margin="130,150,0,0" Name="ok" VerticalAlignment="Top" Width="75" Click="ok_Click" />
<Button Content="Storno" Height="23" HorizontalAlignment="Left" Margin="300,150,0,0" Name="storno" VerticalAlignment="Top" Width="75" Click="storno_Click" IsCancel="True" />
<TextBox Height="21" HorizontalAlignment="Left" Margin="130,24,0,0" Name="nazevVyrobku" VerticalAlignment="Top" Width="340" />
<TextBox Height="21" HorizontalAlignment="Left" Margin="130,64,0,0" Name="mnozstevniJednotka" VerticalAlignment="Top" Width="340" />
<TextBox Height="21" HorizontalAlignment="Left" Margin="130,104,0,0" Name="cenaZaJednotku" VerticalAlignment="Top" Width="150" />
<Label Content="Název Výrobku" Height="28" HorizontalAlignment="Left" Margin="6,20,0,0" Name="label1" VerticalAlignment="Top" Width="120" />
<Label Content="Množstevní jednotka" Height="28" HorizontalAlignment="Left" Margin="6,60,0,0" Name="label2" VerticalAlignment="Top" Width="120" />
<Label Content="Cena za jednotku" Height="28" HorizontalAlignment="Left" Margin="6,100,0,0" Name="label3" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>