CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2010
    Posts
    3

    ComboBox with refresh

    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?

  2. #2
    Join Date
    May 2010
    Posts
    4

    Re: ComboBox with refresh

    I didn't get into your code deeply but I think the concept shuold be changed.
    When you bound the ComboBox to some object you should consider to bound it to object that implement INotifyCollectionChanged (ObservableCollection object for example). Use such object guarantee that when you add or delete an item to the collection, the ComboBox will be refreshed automatic.
    If you still have problems (or you want to see an example) upload your project and I will try to help you

  3. #3
    Join Date
    May 2010
    Posts
    3

    Re: ComboBox with refresh

    Hi ahikamo,
    thanks for your help.
    I put the attachement of the project example in order to understand this kind of problem.
    Thanks in advance.
    Attached Files Attached Files

  4. #4
    Join Date
    May 2010
    Posts
    4

    Resolved Re: ComboBox with refresh

    I download your application and change your code, and now everything work fine.

    I changed the connection in the config file so change it back to your settings.

    Fill free to ask question if something is not clear.
    Attached Files Attached Files

  5. #5
    Join Date
    May 2010
    Posts
    3

    Smile Re: ComboBox with refresh

    your are great.Now I try but I'm sure it works.
    Thanks for my study

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured