跨域DataSource Combobox SelectedItem 绑定
我对数据绑定相当陌生XAML,所以这可能是相当简单的事情,但我已经被它难住好几天了(并且对更多的谷歌搜索感到沮丧,而不是我现在可以跟踪的),并且希望任何正确方向的指示。我唯一的偏好是如果可能的话将其保留在纯 XAML 中。
在我的 RIA SL4 项目中,我有两个实体 PackageOS
和 OS
,其中 PackageOS
通过 OS
与 OS
关联。 code>PackageOS.OS (通过 PackageOS.OSID
<-> OS.ID
关联 - 和 [Include]
+ .Include()
在相关部分正确设置)
这是我在 DataForm 中使用的模板(在 Page.Resource
部分以及所有其他涉及的 DDS 中定义)要绑定到 PackageOS
实体的 OSEntities
列表(来自使用 DDS 的 RIA GetOSEntities()
):
<DataTemplate x:Key="POSItemTemplate">
<StackPanel>
<toolkit:DataField Label="PackageOS.OS">
<TextBlock Text="{Binding Source={StaticResource packageOSEntityDomainDataSource}, Path=Data.CurrentItem.OS}" />
</toolkit:DataField>
<toolkit:DataField Label="OS">
<ComboBox ItemsSource="{Binding Path=Data, Source={StaticResource osEntityDomainDataSource}}"
SelectedItem="{Binding Path=Data.CurrentItem.OS, Source={StaticResource packageOSEntityDomainDataSource}}"/>
</toolkit:DataField>
</StackPanel>
</DataTemplate>
核心问题是 SelectedItem
ComboBox
的 > 不起作用。所有绑定都可以从 IDE 绑定向导访问,因此我输入不正确的路径不是问题。我可以看到 packageOSEntityDomainDataSource.Data.CurrentItem
的类型为 PackageOS
。
如果我在后端数据库中创建手动条目,结果将显示在 PackageOS.OS
文本块中,因此我知道它已正确返回,但 SelectedItem
拒绝拾取它(它最终选择下拉列表中的第一个值,无论 PackageOS
中的 OS
项如何)。
非常感谢!
I'm fairly new to Data binding & XAML, so this probably is fairly simple thing but I've been stumped on it for days now (and frustrated with more googling than i can track at this point) and would appreciate any pointers in the right direction. My only preference is to keep it in pure XAML if possible.
In my RIA SL4 project, I have two Entities PackageOS
and OS
where PackageOS
has an association to OS
through PackageOS.OS
(associating through PackageOS.OSID
<-> OS.ID
- and [Include]
+ .Include()
setup properly on relevant sections)
This is the template (defined in Page.Resource
section along with all other involved DDS) I'm using in DataForm to get OSEntities
List to bind into PackageOS
Entity (coming from RIA GetOSEntities()
using DDS):
<DataTemplate x:Key="POSItemTemplate">
<StackPanel>
<toolkit:DataField Label="PackageOS.OS">
<TextBlock Text="{Binding Source={StaticResource packageOSEntityDomainDataSource}, Path=Data.CurrentItem.OS}" />
</toolkit:DataField>
<toolkit:DataField Label="OS">
<ComboBox ItemsSource="{Binding Path=Data, Source={StaticResource osEntityDomainDataSource}}"
SelectedItem="{Binding Path=Data.CurrentItem.OS, Source={StaticResource packageOSEntityDomainDataSource}}"/>
</toolkit:DataField>
</StackPanel>
</DataTemplate>
The core problem is SelectedItem
of ComboBox
is not working. All the bindings are reachable from IDE Binding wizard so it's not a problem of me typing incorrect path. I can see packageOSEntityDomainDataSource.Data.CurrentItem
to be of type PackageOS
.
If i create a manual entry in backend database, the result is shown in PackageOS.OS
textblock so I know it is properly being returned but SelectedItem
refuses to pick it up (it ends up selecting the first value in dropdown list regardless of OS
item in PackageOS
).
Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于明白了这一点。留下我的答案,希望它可以节省其他人我在这方面花费的时间。
第一课
问题在于,我没有为生成的实体提供自定义的平等实现,并且默认引用平等不起作用,因为我使用了两个不同的实例。一旦我在生成的实体上实现了 IEquatable(通过服务器端的 .shared.cs 部分类),一切就开始像魅力一样工作。
有关详细信息,请参阅 Silverlight ComboBox 控制人口Manishdalal
第二课
如果可以的话,不要使用多个 DDS 控件。特别是在 DDS 上使用写入操作后,在提交更改之前,您无法加载/刷新共享 DomainContext 的任何其他 DDS。上面的链接显示了当您只想选取实体列表来填充 ComboBox 时,如何使用列表生成器来避免多个 DDS。
我的新代码如下所示:
其中
OSListGenerator
从DomainContext< 加载后通过其
OSList
属性返回IEnumerable
/code>第三课
在 DDS DataTemplate 中,您必须明确使用双向绑定。这是新的行为;我花了几天时间才弄清楚,因为我提到的大多数教程都使用 SL3,而且我没有意识到这是 SL4 中 DDS DataTemplate 行为的重大变化。
Finally figured this out. Leaving my answer in hopes that it saves somebody else the time that I spent on this.
First Lesson
The problem was in the fact that I didn't have a custom Equality implementation for generated entities and default reference equality didn't work as I was using two different instances. Once I implemented
IEquatable
on my generated entities (through .shared.cs partial classes on server side) everything started working like a charm.For details please see Silverlight ComboBox Control Population by Manishdalal
Second lesson
Do not use multiple DDS controls if you can help it. Especially once you use a write operation on a DDS, you cannot load/refresh any other DDS that is sharing the DomainContext until changes are committed. The link above shows how to avoid multiple DDS by using list generators when all you want is to pick up list of entities to fill ComboBox up.
My new code looks like this:
Where
OSListGenerator
is returning anIEnumerable<OSEntity>
through itsOSList
property after loading it fromDomainContext
Third Lesson
In DDS DataTemplate you have to be explicit with TwoWay Binding. This is the new behaviour; something that took me days to figure as most of the tutorials I referred to were using SL3 and I didn't realize that this was a breaking change in DDS DataTemplate behaviour in SL4.