无法'绑定到ViewModel

时间:2012-04-24 09:09:04

标签: c# windows-phone-7 silverlight-4.0 mvvm-light

我有一个问题在过去的几个小时内有效,这里是ViewModel代码:(PS:我不能分享网址流,但不要担心它的游行,因为我用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);
                       }

视图:

   <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>

问题是它一切顺利客户即使她已经装好但没有出现!有人有想法吗?

3 个答案:

答案 0 :(得分:1)

您正在将Customers设置为可观察集合的新实例!

当您将可观察集合更改为新实例时,必须使用INotifyPropertyChanged告知视图集合已将更改为新实例 - 尽管会通知对集合中项目的更改,对ITSELF集合的更改不是。

执行此操作时:

Customers = new ObservableCollection<CustomerPublic>(liste);

该视图仍绑定到OLD集合。你应该这样做:

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

或确保Customers属性调用NotifyPropertyChanged函数。

查看此视频或文章了解详情:

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

祝你好运,如果有帮助,请告诉我!!

答案 1 :(得分:0)

我遇到了类似的问题;

public void FillList(List<StockItem> siList)
    {


        listBox.ItemsSource = siList;


    }

其中sIList是X项的填充列表,具有正确命名的属性。 程序构建&amp;运行正常,但列表框没有显示。 (转换到MVVM时会出现此问题)

答案 2 :(得分:0)

我知道了。

检查你的datacontext - 我敢打赌它是null。我在WP7中遇到了同样的问题。在PhoneApplicationPage的构造函数中执行:

DataContext = new Detail_AgenceViewModel();

并在那里初始化它。在WP7中,当我在XAML中创建datacontext时,它为null。这有帮助吗?