0

I'm have two TextBoxes (XAML)

<s:SurfaceTextBox Name="CodePostal" TextChanged="CodePostalChanged" />
<s:SurfaceTextBox Name="Commune" Text="{Binding Path=CommuneGeographique}" />

And in the (.cs file)

private MyModelContainer db;
private ObservableCollection<Commune> _CommunesList = new ObservableCollection<Commune>();
private ObservableCollection<Commune> CommunesList 
{ 
    get { return _CommunesList; }
    set { _CommunesList = value; }
}

In the constructor, I have this :

InitializeComponent();
getCommunes("Test");

And getCommunes(string search), is a Linq query

db = new MyModelContainer();

CommunesList.Clear();
Commune.DataContext = _CommunesList;

var myCommunes = from d in db.Communes
                 where d.CommunePostale.Equals(search)
                 select d;

foreach (Commune c in myCommunes)
{
    CommunesList.Add(c);
}

Commune.DataContext = CommunesList;

At this point everything works fine and the Commune TextBox displays what I want.

My problem is that when I try to call the getCommunes() method on a TextChanged

private void CodePostalChanged(object sender, TextChangedEventArgs textChangedEventArgs)
{
    getCommunes("Toto");
}

Nothing happens and the TextBox is cleared.

(It should display something else, but its empty even if the CommuneList has an element)

Wassim AZIRAR
  • 10,823
  • 38
  • 121
  • 174

1 Answers1

0

because you don't bind your textbox with good property, CommuneGeographique ??

  <s:SurfaceTextBox Grid.Column="1" Name="Commune" Text="{Binding Path=CommuneGeographique}" />
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • It works the first time when I start the app, the problem is that when I type something in the CodePostal TextBox the Commune TextBox is cleared and not filled with the right data !! – Wassim AZIRAR Jul 04 '12 at 15:32
  • Hi Wassim, because your bind is not ajusted, you must to bind with path = name of your property in your objet. for example call your function , set result in property (TestProperty) of object, and bind your textbox with TestProperty , for our example – Aghilas Yakoub Jul 04 '12 at 15:36
  • This is exactly what I'm doing. CommuneGeographique is a property of the db object. If it was not I shouldn't get it work at least one time – Wassim AZIRAR Jul 04 '12 at 15:38
  • Have you implemented INotifyPropertyChanged ? – Aghilas Yakoub Jul 04 '12 at 15:40
  • Nop, can you help me to do that ? – Wassim AZIRAR Jul 04 '12 at 15:57
  • Yes Wassim, go to this site http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial, you have example with SongViewModel – Aghilas Yakoub Jul 04 '12 at 15:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13418/discussion-between-aghilas-yakoub-and-wassim) – Aghilas Yakoub Jul 04 '12 at 16:52