就像标题说的那样,我想将子View中的元素绑定到父ViewModel的属性。
是否有Caliburn.Micro或默认的WPF XAML语法来做到这一点?我的意思是,这种确切的情况是:绑定到父ViewModel的属性。
还是我应该尝试以其他方式实现这一目标?是这样,最好/最简单的方法是什么?
这是一个非常基本的Caliburn.Micro应用程序示例:
MainWindowViewModel.cs
namespace BindingToParentVMProperty.MVVM
{
public class MainWindowViewModel
{
public ChildViewModel Child { get; set; }
public string ParentName { get; set; }
public MainWindowViewModel()
{
Child = new ChildViewModel();
ParentName = "Peter Griffin";
}
}
}
MainWindowView.xaml
:
<UserControl x:Class="BindingToParentVMProperty.MVVM.MainWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Width="200" Height="200">
<ContentControl Name="Child" />
</Grid>
</UserControl>
ChildViewModel.cs
:
using Caliburn.Micro;
namespace BindingToParentVMProperty.MVVM
{
public class ChildViewModel
{
public BindableCollection<GrandChildViewModel> GrandKids { get; set; }
public string ChildName { get; set; }
public ChildViewModel()
{
ChildName = "Stewie Griffin";
GrandKids = new BindableCollection<GrandChildViewModel>
{
new GrandChildViewModel {GrandChildName = "Stewie Griffin Jr."},
new GrandChildViewModel {GrandChildName = "Rupert Griffin Jr."}
};
}
}
}
ChildView.xaml
:
<UserControl x:Class="BindingToParentVMProperty.MVVM.ChildView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<!-- need to bind to MainWindowViewModel.ParentName-->
<TextBlock Text="{Binding Path=HowToBindTo_ParentName}"/>
<ListView Name="GrandKids" />
</StackPanel>
</UserControl>
GrandChildViewModel.cs
:
namespace BindingToParentVMProperty.MVVM
{
public class GrandChildViewModel
{
public string GrandChildName { get; set; }
}
}
GrandChildView.xaml
:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Class="BindingToParentVMProperty.MVVM.GrandChildView"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<TextBlock x:Name="GrandChildName" />
<!-- need to bind to ChildViewModel.ChildName -->
<TextBlock Text="{Binding Path=HowToBindTo_ChildName}"/>
</StackPanel>
</UserControl>
似乎最直接的建议是保留有关孩子的父母的一些记录ViewModel
(例如,Parent
财产)。由于您希望将自己ViewModels
绑定到各自的对象上Views
,因此我认为,如果您开始绑定到属性并包含超出绑定范围的信息,这将在概念上变得非常棘手ViewModel
。
此外,尽管对于您正在处理的特定项目可能并不在意,但是,如果您开始包含other的属性UserControls
,则会隐式创建链接和依赖项,如果您希望重新执行此操作,可能会给您带来不便在非层次结构上下文中使用控件。
也就是说,您可以遍历UI树以从父控件中获取所需的DataContext
(ViewModel
),并使用和属性使用建议的现有WPF
语法将其绑定到所需的属性。Kollegos
RelativeSource
AncestorType
在您的情况下,可能看起来像这样:
<UserControl x:Class="BindingToParentVMProperty.MVVM.ChildView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:YourProjectNamespaceContainingParentView"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<!-- Set our DataContext bind to parent MainWindowViews DataContext (ViewModel) -->
<TextBlock Text="{Binding Path=ParentName}"
DataContext="{Binding RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type local:MainWindowView}},
Path=DataContext}"/>
<ListView Name="GrandKids" />
</StackPanel>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句