wpf向上绑定:绑定到嵌套uiElement中的视图模型属性

编程入门 行业动态 更新时间:2024-10-23 05:48:29
本文介绍了wpf向上绑定:绑定到嵌套uiElement中的视图模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有视图模型和一些嵌套UI元素的WPF项目.这是XAML(的相关部分):

I have a WPF project with a view model and some nested UI elements. Here is the (relevant section of) XAML:

<UserControl> // DataContext is MyVM (set programmatically) <TreeView ItemsSource="{Binding Trees}"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Subtrees}"> <StackPanel> <ListView ItemsSource="{Binding Contents}" SelectedValue="{Binding SelectedContent}" // won't work: Tree has no such property SelectionMode="Single"/> </StackPanel> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> </UserControl>

这是ViewModel类的代码:

Here the code for the ViewModel class:

public class MyVM : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string name = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } public IEnumerable<Tree> Trees { get; set; } private object _selectedContent; public string SelectedContent { get => _selectedContent; set { _selectedContent = value; OnPropertyChanged(); } } }

以下是类 Tree 的代码:

public class Tree { public IEnumerable<Tree> Subtrees { get; set; } public IEnumerable<string> Contents { get; set; } }

我想在全局范围内仅允许所有 ListViews 进行一个选择.就像这里一样,我想将所有 ListViews 绑定到属性 SelectedContent .

I want to allow only one selection globally for all ListViews. Just like here, I want to bind all ListViews to the property SelectedContent in the view model MyVM.

问题是 ListView 的数据上下文是 Tree ,而不是顶级用户控件的 MyVM .(它应该是 Tree ,因为我们要显示 Contents .)我知道我可以使用 SelectedValuePath 向下绑定,但是我该怎么做而是为了将 SelectedValue 绑定到 MyVM 属性 SelectedContent ?

The problem is that the data context of the ListView is a Tree, and not the MyVM from the top user control. (It should be Tree, since we want to show the Contents.) I know I can bind downwards using SelectedValuePath, but how do I go up instead in order to bind SelectedValue to the MyVM property SelectedContent?

我尝试了 SelectedValue =""{Binding RelativeSource = {RelativeSource AncestorType = {x:Type UserControl}},Path = SelectedContent}",但没有用.

I tried SelectedValue="{Binding RelativeSource={RelativeSource AncestorType ={x:Type UserControl}}, Path=SelectedContent}", but it did not work.

推荐答案

在此处有一条评论,说:

只是要在此处说明如果要绑定到那么您必须明确指定RelativeSource的DataContext :{Binding Path = DataContext.SomeProperty,RelativeSource =....这是当我尝试绑定到一个新手时,对我来说有些意外父对象在DataTemplate中的DataContext.

Just wanted to note here that if you want to bind to a property in the DataContext of the RelativeSource then you must explicitly specify it: {Binding Path=DataContext.SomeProperty, RelativeSource=.... This was somewhat unexpected for me as a newbie when I was trying to bind to a parent's DataContext within a DataTemplate.

此评论值得更多关注,因此我将其用作正确答案.

This comment deserves more attention, so I'll use it as the correct answer.

更多推荐

wpf向上绑定:绑定到嵌套uiElement中的视图模型属性

本文发布于:2023-11-12 12:33:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581507.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   嵌套   视图   属性   模型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!