wpf向上绑定:绑定到嵌套uiElement中的视图模型属性 [英] wpf binding upwards: bind to view model property inside nested 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 视图模型
MyVM
中的code>.
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中的视图模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!