Zied Nasr
Zied Nasr

Reputation: 59

can't' bind to ViewModel

I have a problem that resists the past few hours, here is the ViewModel code: (PS: I can not share the url stream but do not worry its march because I tested it with BreakPoint)

private ObservableCollection<CustomerPublic> customers;
    List<CustomerPublic> liste = new List<CustomerPublic>();
    public ObservableCollection<CustomerPublic> Customers
    {
        get
        { return customers; }
        set
        {
            if (customers != value)
            {
                customers = value;
                RaisePropertyChanged("Customers");
            }
        }
    }
    private int id;
    public int ID
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
            RaisePropertyChanged("ID");
        }
    }
    public Detail_AgenceViewModel(int id)
    {
        this.ID = id;
        PopulateCollection();  
    }
    public Detail_AgenceViewModel()
    {

    }

    private void PopulateCollection()
    {
        ParseFeedRequest();
    }


    private void ParseFeedRequest()
    {
        RestClient client = new RestClient();
        client.BaseUrl = "....";

        RestRequest request = new RestRequest();

        .......

        client.ExecuteAsync(request, ParseFeedCallBack);
    }

    public void ParseFeedCallBack(IRestResponse response)
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            ParseXMLFeed(response.Content);
        }
    }

    private void ParseXMLFeed(string feed)
    {
        if (feed == null)
            return;
        XElement xmlItems = XElement.Parse(feed);

        liste = (from response in xmlItems.Descendants("result")
                 let lib = response.Element("lib")
                 let adresse = response.Element("adresse")

                 select new CustomerPublic
                 {
                     lib = lib == null ? null : lib.Value,
                     adresse = adresse == null ? null : adresse.Value,


                 }).ToList();

        Customers = new ObservableCollection<CustomerPublic>(liste);
                       }

the View:

   <phone:PhoneApplicationPage.DataContext>
    <vm:Detail_AgenceViewModel/>
  </phone:PhoneApplicationPage.DataContext>
    <Grid x:Name="LayoutRoot"
      Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel"
                Grid.Row="0"
                Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle"
                   Text="MY APPLICATION"
                   Style="{StaticResource PhoneTextNormalStyle}" />
        <TextBlock x:Name="PageTitle"
                   Text="page name"
                   Margin="9,-7,0,0"
                   Style="{StaticResource PhoneTextTitle1Style}" />
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <StackPanel x:Name="ContentPanel" Grid.Row="2" Margin="12,0,12,0" Orientation="Vertical">

        <!--TextBox Text="{Binding Count, Mode=TwoWay}" x:Name="tbCount" />
        <TextBlock Text="{Binding Count}" /-->
        <ListBox x:Name="Agences" ItemsSource="{Binding Customers}"  >

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding lib}" />
                        <TextBlock Text="{Binding adresse}" />


                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
  </Grid>

The problem is that its all going well Customers even she is loaded but nothing appears! someone has an idea?

Upvotes: 1

Views: 304

Answers (3)

Dave Kerr
Dave Kerr

Reputation: 5297

I've got it.

Check your datacontext - I bet it is null. I've had this exact same issue in WP7. In the constructor of your PhoneApplicationPage do:

DataContext = new Detail_AgenceViewModel();

and initialise it there. In WP7 when I create the datacontext in XAML it's null. Does this help?

Upvotes: 0

NoxBene
NoxBene

Reputation: 94

I'm having similar problems ;

public void FillList(List<StockItem> siList)
    {


        listBox.ItemsSource = siList;


    }

Where sIList is a filled list of X items, with correctly named properties. Program builds & runs fine, but the listbox isnt shown. (this problem started when transitioning into MVVM)

Upvotes: 0

Dave Kerr
Dave Kerr

Reputation: 5297

You are setting Customers to a new instance of an observable collection!

When you change the observable collection to a new instance, you must use INotifyPropertyChanged to tell the view that the collection has changed to a new instance - although changes to the items IN the collection are notified, changes to the collection ITSELF are not.

When you do this:

Customers = new ObservableCollection<CustomerPublic>(liste);

The view is still bound to the OLD collection. You should do:

Customers.Clear();
foreach(var item in liste)
  Customers.Add(item);

OR make sure that the Customers property calls the NotifyPropertyChanged function.

Have a check of this video or article for more info:

http://www.codeproject.com/Articles/371217/Apex-Part-1-Create-Your-First-MVVM-Application http://www.youtube.com/watch?v=m4cx9w5fiwk&feature=youtu.be

Good luck and please do let me know if this helps!!

Upvotes: 1

Related Questions