-1

before all, I'm sorry but english is not my native. So, i am developing a wpf application, with thow windows, one present as many button as records on my db and another with a little form to add a new one, but i have one problem. When I add another record to my db OnPropertyChanged does not fire and the new buttons doesn't show up, i think it is because the DataContext.

LugaresWindow.xaml

 <ItemsControl ItemsSource="{Binding LugaresBtns}" Margin="0,35,0,0">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="7" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Button Content="{Binding numero}" Click="openLugar" Tag="{Binding idLugar}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="96" Height="30"/>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

LugaresWindow.xaml.cs

private void addlugarbtn_Click(object sender, RoutedEventArgs e)
{
    //(DataContext as LugarViewService).addLugar("11","6969","A","","1"); **IF I USE IT LIKE THIS IT WORKS!**
    EditLugarWindow window = new EditLugarWindow(-1);
    window.Show(); 
}

EditLugarWindow.xaml.cs (confirm the form to add a new record)

        if (lvs.addLugar(idparquetxt.Text, numerotxt.Text, sectortxt.Text, matriculatxt.Text, selected))
        {
            this.Close();
        }

And on my LugarViewService.cs

   public bool addLugar(string idParque, string numero, string sector, string matricula, string tipo)
    {
        int newnumero, newtipo, newparque;

        try
        {
            newparque = int.Parse(idParque);
            newnumero = int.Parse(numero);
            newtipo = int.Parse(tipo);
            pmclient.addLugar(newparque, newnumero, sector, matricula, newtipo, 1);               
            OnPropertyChanged("LugaresBtns");
            return true;
        }
        catch (Exception e)
        {
            MessageBox.Show("Erro ao adicionar. Por favor verifique o numero de lugar ou o tipo\n" + e.Message);
            return false;
        }
    }


public List<Lugar> LugaresBtns
{
    get
    {
        return pmclient.getLugaresByParque(idParque).ToList();

    }
}

So, as you can see, the LugaresWindow.xaml doesnt get updated after i insert a new record using another window. What am I doing wrong?

Thank you all!

  • do you have a viewmodel for LugaresWindow ? – Saifeddine M Jan 16 '16 at 23:35
  • yes, it is the LugarViewService.cs – Ricardo Sousa Jan 16 '16 at 23:39
  • did you assign it to the datacontext of LugaresWindow ? – Saifeddine M Jan 16 '16 at 23:40
  • 2
    I think that you should use ObservableCollection https://msdn.microsoft.com/en-us/library/ms668604%28v=vs.110%29.aspx , an explanation about it http://stackoverflow.com/questions/4279185/what-is-the-use-of-observablecollection-in-net – ganchito55 Jan 16 '16 at 23:44
  • in `lvs.addLugar`, what is lvs? Are you using same instance of ViewModel? You have to call addLugar on instance used in LugaresWindow. – Shadowed Jan 17 '16 at 02:10
  • @Shadowed how can I do that? – Ricardo Sousa Jan 17 '16 at 11:52
  • I don't know what is `lvs` in your current code, as you pasted only part where you are using it and not where you are assigning it, but, you could for example, in `addlugarbtn_Click` set window.DataContext to same instance of LugarViewService you are using in your main window. Then use that instead of `lvs`. If this doesn't help, please edit your question and add more complete code snippet so we can better see what you already have. – Shadowed Jan 17 '16 at 13:06

1 Answers1

0

It's solved, i just had to set the window.DataContext to the lvs instance i was using. thak you all for the replies.