简体   繁体   English

WP7将列表框绑定到WCF结果

[英]WP7 Binding Listbox to WCF result

I have a WCF call that returns a list of objects. 我有一个WCF调用,它返回对象列表。

I've created a WP7 Silverlight Pivot application and modified the MainViewModel to load data from my WCF service, the LoadData method now looks like this 我已经创建了WP7 Silverlight Pivot应用程序,并修改了MainViewModel以从WCF服务加载数据,LoadData方法现在看起来像这样

public ObservableCollection<Standing> Items { get; private set; }

public void LoadData()
{
    var c = new WS.WSClient();
    c.GetStandingsCompleted += GetStandingsCompleted;
    c.GetStandingsAsync();            
}

void GetStandingsCompleted(object sender, GetStandingsCompletedEventArgs e)
{
    Items = e.Result;
    this.IsDataLoaded = true;
}

This runs and if I put a break point on the completed event I can see it ran successfully and my Items collection now has 50 odd items. 这会运行,如果我在完成的事件上设置一个断点,我可以看到它成功运行,并且我的Items集合现在有50个奇数项目。 However the listbox in the UI doesnt display these. 但是,UI中的列表框不会显示这些。

If I add the following line to the bottom of my LoadData method then I see 1 item appear in the listbx on the UI 如果将以下行添加到我的LoadData方法的底部,那么我会在UI的listbx中看到1个项目

Items.Add(new Standing(){Team="Test"});

This proves the bindings are correct but it seems that because of the delay in the Asynch WCF call the UI doesn't update. 这证明了绑定是正确的,但是由于Asynch WCF调用的延迟,UI并未更新。

For reference I've updates the MainPage.xaml listbox to bind to the Team property on my Standing object 作为参考,我更新了MainPage.xaml列表框以绑定到我的Standing对象上的Team属性。

<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
          <StackPanel Margin="0,0,0,17" Width="432">
                <TextBlock Text="{Binding Team}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                <TextBlock Text="{Binding Team}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
          </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Any ideas on what I'm doing wrong? 关于我在做什么错的任何想法吗?

Thanks 谢谢

When your ListBox is first created its ItemsSource property takes the current value of Items which is null . 首次创建ListBoxItemsSource属性采用Items的当前值,该值为null When your WCF call completes and you assign a new value to Items , the view has no way of knowing that the value of that property has changed without you firing a PropertyChanged event, as Greg Zimmers mentions in his answer. 当您完成WCF调用并将新值分配给Items ,如您无法触发PropertyChanged事件,视图就无法知道该属性的值已更改,正如Greg Zimmers在回答中提到的那样。

Since you're using an ObservableCollection , an alternative method would be to initially create an empty collection and then add objects to it when the WCF call completes. 由于您使用的是ObservableCollection ,因此另一种方法是首先创建一个空集合,然后在WCF调用完成时向其添加对象。

private ObservableCollection<Standing> _items = new ObservableCollection<Standing>();
public ObservableCollection<Standing> Items
{ 
  get { return _items; } 
  private set;
}


void GetStandingsCompleted(object sender, GetStandingsCompletedEventArgs e)
{
    foreach( var item in e.Result ) Items.Add( item );
    this.IsDataLoaded = true;
}

您的数据实体“站立”是否实现INotifyPropertyChanged接口,并且是否引发属性更改事件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM