Hi
I'm making a short application to study wpf and linq to sql. I put a combobox, a text box and a button. When I click on the button, the text I've inserted on the text box has put on the dbo.table. The combobox should show the records of the dbo.table, but I see the new records only when I close and reopen the application.
the XAML code is this:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="289" Width="209" Loaded="Window_Loaded">
<Window.Resources>
<DataTemplate x:Key="SuppliersTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=IDPaese}" />
<TextBlock Text=" : " />
<TextBlock Text="{Binding Path=NomePaese}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ComboBox Height="23" Margin="0,20,0,0" Name="PaesiCombo" VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource SuppliersTemplate}" SelectionChanged="PaesiCombo_SelectionChanged" />
<TextBox Height="23" Name="inserisciValoreTextBox" VerticalAlignment="Top" TextChanged="inserisciValoreTextBox_TextChanged" Margin="20"/>
<Button Name="button1" Click="button1_Click">Insert</Button>
</StackPanel>
</Window>
and the cs code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Linq;
using System.Data.Linq.SqlClient;
namespace WpfApplication1
{
/// <summary>
/// Logica di interazione per Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private DataClasses1DataContext connection = null;
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
connection = new DataClasses1DataContext();
this.PaesiCombo.DataContext = connection.Paesi;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Table<Paesi> insNomePaese = connection.Paesi;
Paesi insPAese= new Paesi();
insPAese.NomePaese = this.inserisciValoreTextBox.Text;
insNomePaese.InsertOnSubmit(insPAese);
connection.SubmitChanges();
connection.Refresh(RefreshMode.OverwriteCurrentValues,insNomePaese);
this.PaesiCombo.DataContext = connection.Paesi;
this.inserisciValoreTextBox.Clear();
this.button1.IsEnabled = false;
}
private void inserisciValoreTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
this.button1.IsEnabled = true;
}
private void PaesiCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(((Paesi)this.PaesiCombo.SelectedItem).IDPaese.ToString());
}
}
}
I'didn't understand how to refresh the combobox in order to show the new records. Someone can help me with this?