所选项目的所选项目

发布于 2024-11-27 13:46:42 字数 721 浏览 0 评论 0原文

首先,我要感谢您在这个论坛上阅读的许多好帖子。不幸的是,我无法为我当前的问题找到任何帮助(无论是在这里还是其他任何地方)。

我想做的事情听起来很简单,但我不知道如何让它工作......也许我对 wpf 还很陌生,或者我对 wpfy 还不够:)

我正在设计一个前端自动化制造中的零件结束:

  • 我有很多可以放置托盘的地方(但也可以是空的)。
  • 每个托盘最多有 3 个可以安装零件的位置。
  • 一切都是动态创建的数据库并对变化做出反应。
    • 托盘上零件的位置也来自数据库,并且应该可视化

我想要的是:

  • 通过托盘预览对托盘位置进行概述
  • 当我选择我想要的位置时查看该位置的详细视图
  • 当我单击详细托盘的托盘上的某个零件时,我想查看该零件的详细信息

前两点非常简单并且工作得很好:

  • 我为每个组件(零件)提供了一个 DataTemplate 、托盘、托盘放置)。实际上,这些是作为数据模板导入的用户控件,
  • 概述是一个列表框,其位置作为
  • 详细信息位置视图的数据上下文我使用用户控件并将其绑定到列表框的 SelectedItem

我尝试将文本块的文本绑定到所选零件的 ID ...并且失败。

也许我可以在后面的代码中使用一些全局变量 - 但这听起来很丑陋。

有人可以帮忙吗?

first of all I would like to thank you for the many good posts that i read in this forum. Unluckily I could not find anything of help for my current problem (either here or anywhere else).

What I'm trying to do sounds quite simple, but I have no clue how to get it to work ... perhaps I'm still to new to wpf or I don't thing wpfy enough :)

I'm designing a front end for a part in an automated manufacturing:

  • I have a quantity of places where pallets can be put (but it can be empty as well).
  • Each pallet has up to 3 places where parts can be mounted
  • Everything is created dynamically of a database and is reacting to changes.
    • The position of the parts on the pallet comes from the database as well and should be visualized

What I would like to have is:

  • An overview over the pallet-places with a preview of the pallet
  • When I select a place I want to see a detail view of the place
  • When I click on a part on the pallet of the detailed pallet I want to see details to the part

The first two points are quite simple and work nicely:

  • I have got a DataTemplate for every component (part, pallet, pallet-place). Actually those are UserControls that are imported as Datatemplates
  • the overview is a ListBox with the places as DataContext
  • for the detail-place-view I use the UserControl and bound it to the SelectedItem of the Listbox

I tried to bind the Text of a Textblock to the ID of the selected Part ... and fail.

Probably I could use some global variables in the code behind - but that sound very ugly.

Can anybody help?

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

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

发布评论

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

评论(3

ペ泪落弦音 2024-12-04 13:46:43

上下文很重要,如果我使用 ContentControl 并将其内容绑定到 SelectedItem ,如下所示:

<ContentControl Content="{Binding SelectedItem, ElementName=mylistbox}">

我可以像这样绑定到 DataTemplate 中所选项目的 ID:

<ContentControl.ContentTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding ID}" />
    </DataTemplate>
</ContentControl.ContentTemplate>

那是因为设置 ContentControl 的 Content 也会自动设置 DataContext,并且此绑定是相对于 DataContext 的,因为没有源 (ElementName, 相对来源来源)已指定。

我不知道您的 UserControl 如何处理上下文,如果 DataContext 不受影响,则此类绑定将不起作用。那么您需要直接绑定:

<uc:MyDetailsView Data="{Binding SelectedItem, ElementName=mylistbox}">
    <!-- ... -->
        <TextBlock Text="{Binding SelectedItem.ID, ElementName=mylistbox}" />

这当然违背了在 UserControl 本身上进行绑定的目的。但是除非您发布一些相关代码,否则很难判断出了什么问题。

另请检查 VisualStudio 中的“输出”窗口,绑定错误将显示在那里,并可能提供有关出错原因的有价值的信息。

Context is important, if i use a ContentControl and bind its content to the SelectedItem like this:

<ContentControl Content="{Binding SelectedItem, ElementName=mylistbox}">

I can bind to the ID of the selected item in the DataTemplate like this:

<ContentControl.ContentTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding ID}" />
    </DataTemplate>
</ContentControl.ContentTemplate>

That is because setting the Content of the ContentControl automatically sets the DataContext as well, and this binding is relative to the DataContext since no source (ElementName, RelativeSource, Source) has been specified.

I do not know how your UserControl handles the context, if the DataContext is not affected such bindings will not work. You would need to bind directly then:

<uc:MyDetailsView Data="{Binding SelectedItem, ElementName=mylistbox}">
    <!-- ... -->
        <TextBlock Text="{Binding SelectedItem.ID, ElementName=mylistbox}" />

This of course defeats the purpose of having the binding on the UserControl itself in the first place. But unless you post some relevant code it's quite hard to tell what is wrong.

Also check the Output window in VisualStudio, binding errors will show up there and might provide valuable information as to what went wrong.

记忆里有你的影子 2024-12-04 13:46:42

我有一个解决方案...虽然不太好但有效。

  • 我在托盘中创建了一个事件,当选定的零件位置发生更改时,该事件会
  • 触发我在托盘位置中处理事件并创建一个新事件
  • 最后,我在概述中处理它并相应地更改详细视图

很可能有很多更好的解决方案,但这就足够了。

I have got a solution ... it is not nice but works.

  • I created an event in the pallet, that triggers, when the selected part-place changes
  • I handle the event in the pallet-place and create a new one
  • And finally I handle it in the overview and change the detailview accordingly

Most likely there are much nicer solutions, but it will suffice.

溇涏 2024-12-04 13:46:42

也许尝试 ElementName 绑定?

<TextBlock Text="{Binding ElementName=Name_of_your_Listbox, Path=SelectedItem.ID" />

您可以发布更多 TextBlock 和 Binding 的代码吗?

Perhaps try an ElementName binding?

<TextBlock Text="{Binding ElementName=Name_of_your_Listbox, Path=SelectedItem.ID" />

Can you post a bit more code of your TextBlock and your Binding?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文