CollectionViewSource 将源绑定到 Web 服务(使用 WCF RIA 服务和 LINQ) - Silverlight [英] CollectionViewSource bind source to webservice (using WCF RIA services and LINQ) - Silverlight
问题描述
我有一个小问题,可能很愚蠢,但我想不通..无论如何,我想要做的就是将我的 CollectionViewSource.Source 绑定到我在 Silverlight 4 中使用 WCF RIA 服务生成的查询.
I have a small problem, probably stupid but I can't figure this out.. Anyways all I'm trying to do is bind my CollectionViewSource.Source to my resulting query in Silverlight 4 with WCF RIA services.
这是我的 XAML :
This is my XAML :
<UserControl.Resources>
<CollectionViewSource x:Name="cvsServiceTypes" />
</UserControl.Resources>
<Grid>
<ComboBox Height="23" HorizontalAlignment="Left" ItemsSource="cvsServiceTypes" DisplayMemberPath="Type" SelectedValuePath="IDServicesType" Margin="154,51,0,0" Name="cbServiceType" VerticalAlignment="Top" Width="120" SelectedValue="{Binding fkservicetype, Mode=TwoWay}" />
</Grid>
还有我的代码隐藏:
public Services()
{
InitializeComponent();
webService.GetServiceTypesCompleted += (s, e) => { cvsServiceTypes.Source = e.Result; };
webService.GetServiceTypesAsync();
}
但它似乎不起作用......我做错了什么?
But it doesn't seem to work... what am I doing wrong?
非常感谢!
推荐答案
我希望你不介意我忽略 web 服务调用部分 - 看起来你正在努力将你的项目绑定到 ComboBox,所以就是我要解决的部分.
I hope you don't mind if I ignore the web service call part - it looks like you're struggling with binding your items to the ComboBox, so that's the part I'll address.
您需要执行以下操作:
- 创建一个 ObservableCollection 属性来包含您的项目.
- 将 CollectionViewSource.Source 绑定到 ObservableCollection.
- 将 ComboBox.ItemsSource 绑定到 CollectionViewSource.
- 在您的 UserControl 上设置 DataContext.
这是一个例子:
<UserControl ...>
<StackPanel>
<StackPanel.Resources>
<CollectionViewSource x:Key="cvs" Source="{Binding Path=Items}"></CollectionViewSource>
</StackPanel.Resources>
<ComboBox ItemsSource="{Binding Source={StaticResource ResourceKey=cvs}}" />
<ComboBox ItemsSource="{Binding Source={StaticResource ResourceKey=cvs}}" />
<ComboBox ItemsSource="{Binding Source={StaticResource ResourceKey=cvs}}" />
</StackPanel>
</UserControl>
代码:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.DataContext = this;
_items.Add("a");
_items.Add("b");
}
private ObservableCollection<string> _items = new ObservableCollection<string>();
public ObservableCollection<string> Items
{
get { return _items; }
set { _items = value; }
}
}
当您的网络服务调用完成时,您可以将项目放入集合中:
You can put items into the collection when your web service call completes like this:
webService.GetServiceTypesCompleted += (s, e) =>
{
foreach (string s in e.result)
{
_items.Add(s);
}
};
这篇关于CollectionViewSource 将源绑定到 Web 服务(使用 WCF RIA 服务和 LINQ) - Silverlight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!