所选项目的所选项目
首先,我要感谢您在这个论坛上阅读的许多好帖子。不幸的是,我无法为我当前的问题找到任何帮助(无论是在这里还是其他任何地方)。
我想做的事情听起来很简单,但我不知道如何让它工作......也许我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
上下文很重要,如果我使用 ContentControl 并将其内容绑定到
SelectedItem
,如下所示:我可以像这样绑定到 DataTemplate 中所选项目的 ID:
那是因为设置
也会自动设置 DataContext,并且此绑定是相对于ContentControl
的 ContentDataContext
的,因为没有源 (ElementName
,相对来源
,来源
)已指定。我不知道您的
UserControl
如何处理上下文,如果DataContext
不受影响,则此类绑定将不起作用。那么您需要直接绑定:这当然违背了在 UserControl 本身上进行绑定的目的。但是除非您发布一些相关代码,否则很难判断出了什么问题。
另请检查 VisualStudio 中的“输出”窗口,绑定错误将显示在那里,并可能提供有关出错原因的有价值的信息。
Context is important, if i use a ContentControl and bind its content to the
SelectedItem
like this:I can bind to the ID of the selected item in the DataTemplate like this:
That is because setting the
Content
of theContentControl
automatically sets the DataContext as well, and this binding is relative to theDataContext
since no source (ElementName
,RelativeSource
,Source
) has been specified.I do not know how your
UserControl
handles the context, if theDataContext
is not affected such bindings will not work. You would need to bind directly then: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.
我有一个解决方案...虽然不太好但有效。
很可能有很多更好的解决方案,但这就足够了。
I have got a solution ... it is not nice but works.
Most likely there are much nicer solutions, but it will suffice.
也许尝试 ElementName 绑定?
您可以发布更多 TextBlock 和 Binding 的代码吗?
Perhaps try an ElementName binding?
Can you post a bit more code of your TextBlock and your Binding?