RIA 服务 - 获取列表项中的 DataContext

发布于 2024-08-04 11:44:13 字数 619 浏览 2 评论 0原文

我正在为我的 RIA 服务项目寻找解决方法,该项目有一个列表框,其中列表项作为用户控件定义为 ItemTemplate,如下所示:

<ListBox x:Name="lstMain">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel Orientation="Horizontal">
           <foo:ListItemDetail />
       </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

列表框绑定到代码隐藏中的域服务。 列表项的类型为ListItemDetail,它具有可编辑字段和更新按钮。 我想调用域服务(父列表的数据上下文)来 SaveChanges() 并在单击按钮时更新项目,但无法访问填充列表的(原始)域服务。 有一个优雅的解决方案吗?

我正在尝试使用 Listbox 标记项来存储域上下文,但我无法从 ListItem 访问它。我得到了父 StackPanel,但它的父级为空。

提前致谢, 拉

I'm looking for a workaround for my RIA Services project, which has a Listbox with the Listitems as a user control defined as an ItemTemplate, like this:

<ListBox x:Name="lstMain">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel Orientation="Horizontal">
           <foo:ListItemDetail />
       </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

The Listbox is bound to a Domain Service in the code-behind.
The List items are of type ListItemDetail, which has editable fields and an Update button.
I'd like to call the Domain Service (the parent Lists's Data Context) to SaveChanges() and update the item when the button is clicked, but there is no way to get to the (original) Domain Service that populated the list.
Is there an elegant solution for this?

I am trying to use the Listbox tag item to store the domain context, but I am unable to get to it from a ListItem. I get the parent StackPanel, but it's parent is null.

Thanks in advance,
Ra

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-08-11 11:44:13

您可以在 ListItemDetail 控件上创建属性。然后您可以将该属性绑定到 ListBox 的 DataContext

<ListBox x:Name="lstMain">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel Orientation="Horizontal">
           <foo:ListItemDetail MyRiaContext="{Binding DataContext, ElementName=lstMain}"/>
       </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

You can create a property on the ListItemDetail control. Then you can bind that property to the ListBox's DataContext

<ListBox x:Name="lstMain">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel Orientation="Horizontal">
           <foo:ListItemDetail MyRiaContext="{Binding DataContext, ElementName=lstMain}"/>
       </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文