SL 从 ListItem 数据模板内向 MainPage.xaml 的 ViewModel 触发命令

发布于 2024-11-09 14:52:10 字数 888 浏览 0 评论 0原文

我可能会让这变得比必要的更复杂......但是这里是。

我有我的 MainPage.xaml,其中我引用了另外两个 UserControl、ResultsView 和 DetailsView。

在 ResultsView 内部,我有一个绑定到自定义项目的 ObservableCollection 的 ListBox,我有一个正在渲染每个项目的 DataTemplate。该项目有一个 CaseID,当我单击它时,它会显示为 HyperlinkBut​​ton,我需要在 MainPageViewModel 中设置的命令才能触发,该命令处理更改可见性以隐藏 ResultsView 并显示 DetailsView。

如何将超链接按钮的命令绑定到 MainPageViewModel 中的命令?

提前致谢!

编辑以澄清:

MainPage.xaml

<resultsView:ResultsView/>
<detailsView:DetailsView/>

两个视图(ResultsView 和 DetailsView)中的每一个都有自己的 ViewModel。因此,我要从位于 ResultsView 内的 ListBox 中的 DataTemplate 开始,我需要转到 MainPageViewModel,这比您提到的答案多了一个步骤。尝试使用您的方法将我的 MainPage.xaml 命名为 Name="mainPage",并将其用作我的 HyperlinkBut​​ton 中的 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 技术交流群。

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

发布评论

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

评论(1

不寐倦长更 2024-11-16 14:52:10

嗯,这取决于具体的细节,不是所有你告诉我们的,但我可以给你一些例子。

假设自定义项的 DataTemplate 位于 ResultsView 用户控件的 XAML 中。这是一个很好的位置,但您可能已将其放入资源字典中。

正如您所说,ListBox 绑定到自定义项的集合。我们进一步假设自定义项集合本身就是 MainPageViewModel 中的一个属性。而且您已经明确表示您要绑定的命令也在 MainPageViewModel 中。

因此,一个非常常见的问题是,您正在与集合绑定关联的模板内工作,因此您的 DataContext 是一个自定义项。它不再是主视图模型本身。这非常好,因为您可以显示自定义项目的适当属性,例如 CaseID。但是,当您想要转义到视图模型的顶层时,这就不太好了。

如果我所说的是真的,那么 ResultsView 用户控件实际上可能绑定到 MainPageViewModel 因为您还没有“深入”自定义项目集合。因此,您需要做的是找到一种使用绑定语法从 ListBoxDataTemplate 内部引用 ResultsView 用户控件的方法。如果你能做到这一点,那么你就逃脱了收藏。

有两种主要方法可以实现此目的:

  • ElementName 语法
  • RelativeSource 语法

我将描述 ElementName 语法,您可以查找另一种语法。

第 1 部分)将您的 ResultsView UserControl 元素命名如下:

<UserControl ....
    Name="resultsView">
    <!-- ... -->

第 2 部分)在定义超链接外观的 DataTemplate 中,使用 < code>ElementName 语法来引用该元素:

<TextBlock>
    <Hyperlink Command="{Binding DataContext.ItemDetailsCommand, ElementName=resultsView}"/>
</TextBlock>

所以首先我们使用 ElementName 来获取 ResultsView UserControl 元素,然后我们有一条包含两部分的路径:第一部分是ResultsViewDataContext 属性为我们提供了 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 the ResultsView 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 your MainPageViewModel. And you've said clearly that the command you want to bind to is also in MainPageViewModel.

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 like CaseID. 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 the MainPageViewModel 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 the ResultsView user control from inside the DataTemplate for the ListBox. If you can do that, then you've escaped the collection.

There are two main approaches to do this:

  • ElementName syntax
  • RelativeSource syntax

I'll describe ElementName syntax and you can look up the other one.

Part 1) Name your ResultsView UserControl element like this:

<UserControl ....
    Name="resultsView">
    <!-- ... -->

Part 2) Inside your DataTemplate where you are defining the appearance of the hyperlink use the ElementName syntax to refer to that element:

<TextBlock>
    <Hyperlink Command="{Binding DataContext.ItemDetailsCommand, ElementName=resultsView}"/>
</TextBlock>

So first we use ElementName to get the ResultsView UserControl element, and then we have a path with two pieces: the first piece is the DataContext property of the ResultsView which gives us the MainPageViewModel (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.

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