SL 从 ListItem 数据模板内向 MainPage.xaml 的 ViewModel 触发命令
我可能会让这变得比必要的更复杂......但是这里是。
我有我的 MainPage.xaml,其中我引用了另外两个 UserControl、ResultsView 和 DetailsView。
在 ResultsView 内部,我有一个绑定到自定义项目的 ObservableCollection 的 ListBox,我有一个正在渲染每个项目的 DataTemplate。该项目有一个 CaseID,当我单击它时,它会显示为 HyperlinkButton,我需要在 MainPageViewModel 中设置的命令才能触发,该命令处理更改可见性以隐藏 ResultsView 并显示 DetailsView。
如何将超链接按钮的命令绑定到 MainPageViewModel 中的命令?
提前致谢!
编辑以澄清:
MainPage.xaml
<resultsView:ResultsView/>
<detailsView:DetailsView/>
两个视图(ResultsView 和 DetailsView)中的每一个都有自己的 ViewModel。因此,我要从位于 ResultsView 内的 ListBox 中的 DataTemplate 开始,我需要转到 MainPageViewModel,这比您提到的答案多了一个步骤。尝试使用您的方法将我的 MainPage.xaml 命名为 Name="mainPage",并将其用作我的 HyperlinkButton 中的 ElementName,但没有运气。我将对相对源选项进行一些研究,看看是否可以实现该功能。
感谢您迄今为止的帮助。
编辑 2:忘记添加 DataTemplate 位于 ResourceDictionary 中,而不是位于 ResultsView 中。
I may be making this more complicated than necessary ... but here goes.
I have my MainPage.xaml, in there I have references to two other UserControl's, ResultsView, and DetailsView.
Inside of the ResultsView, I have a ListBox bound to an ObservableCollection of custom items, I have a DataTemplate that is rendering each item. The item has a CaseID, and when I click on it, it's displayed as a HyperlinkButton, I need a Command I've set in the MainPageViewModel to fire, which handles changing the visibility to hide the ResultsView, and show the DetailsView.
How do I bind the Command of the Hyperlinkbutton to the Command located in my MainPageViewModel?
Thanks in advance!
edit for clarification:
MainPage.xaml
<resultsView:ResultsView/>
<detailsView:DetailsView/>
Each of the two views (ResultsView & DetailsView) have their own ViewModel. So I'm going from my DataTemplate which resides in a ListBox inside my ResultsView, I need to go up to the MainPageViewModel, an extra step than your answer mentioned. Tried your method naming my MainPage.xaml to Name="mainPage", and using that as the ElementName in my HyperlinkButton, but no luck. I'll do some research on the RelativeSource option and see if I can make that work.
Thanks for your help so far.
edit 2: Forgot to add that the DataTemplate is in a ResourceDictionary, not in the ResultsView.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,这取决于具体的细节,不是所有你告诉我们的,但我可以给你一些例子。
假设自定义项的
DataTemplate
位于ResultsView
用户控件的 XAML 中。这是一个很好的位置,但您可能已将其放入资源字典中。正如您所说,
ListBox
绑定到自定义项的集合。我们进一步假设自定义项集合本身就是MainPageViewModel
中的一个属性。而且您已经明确表示您要绑定的命令也在MainPageViewModel
中。因此,一个非常常见的问题是,您正在与集合绑定关联的模板内工作,因此您的
DataContext
是一个自定义项。它不再是主视图模型本身。这非常好,因为您可以显示自定义项目的适当属性,例如CaseID
。但是,当您想要转义到视图模型的顶层时,这就不太好了。如果我所说的是真的,那么
ResultsView
用户控件实际上可能绑定到MainPageViewModel
因为您还没有“深入”自定义项目集合。因此,您需要做的是找到一种使用绑定语法从ListBox
的DataTemplate
内部引用ResultsView
用户控件的方法。如果你能做到这一点,那么你就逃脱了收藏。有两种主要方法可以实现此目的:
ElementName
语法RelativeSource
语法我将描述
ElementName
语法,您可以查找另一种语法。第 1 部分)将您的
ResultsView
UserControl
元素命名如下:第 2 部分)在定义超链接外观的
DataTemplate
中,使用 < code>ElementName 语法来引用该元素:所以首先我们使用
ElementName
来获取ResultsView
UserControl
元素,然后我们有一条包含两部分的路径:第一部分是ResultsView
的DataContext
属性为我们提供了MainPageViewModel
(是的!),然后是我们要调用的命令的属性。这是“转义”在视图模型的更高级别中找到的绑定和发出命令的一种方法。
Well, it depends on the specific details, not all of which you've told us, but I can give you some examples.
Let's say that your
DataTemplate
for the custom items resides in the XAML of theResultsView
user control. That's a good spot for it but you might have put it in a resource dictionary.As you say, the
ListBox
is bound to the collection of custom items. Let's further assume that the custom items collection is itself a property in yourMainPageViewModel
. And you've said clearly that the command you want to bind to is also inMainPageViewModel
.So the problem, which is a very common one, is that you are working inside a template associated with a binding to a collection, and so your
DataContext
is a custom item. It is no longer the main view-model itself. That's great as you show appropriate properties of the custom item likeCaseID
. But it's not great when you want to escape to the top-level of view-model.If what I've said is true, then the
ResultsView
user control is in fact probably bound to theMainPageViewModel
because you haven't "drilled into" the custom items collection yet. So, what you need to do is find a way using the binding syntax to reference theResultsView
user control from inside theDataTemplate
for theListBox
. If you can do that, then you've escaped the collection.There are two main approaches to do this:
ElementName
syntaxRelativeSource
syntaxI'll describe
ElementName
syntax and you can look up the other one.Part 1) Name your
ResultsView
UserControl
element like this:Part 2) Inside your
DataTemplate
where you are defining the appearance of the hyperlink use theElementName
syntax to refer to that element:So first we use
ElementName
to get theResultsView
UserControl
element, and then we have a path with two pieces: the first piece is theDataContext
property of theResultsView
which gives us theMainPageViewModel
(yeah!), and then the property of the command we want to invoke.That's one way to "escape" the binding and issue commands found at a higher level in the view-model.