我想知道这是否是 .NET 的标准功能:在 中设置 时DataContext
,ParentView
它会过滤到所有child views
.
假设你有 ParentView、ChildView1 和 ChildView2:
<UserControl x:Class="DXWPFApplication1.ParentView"
xmlns:view="clr-namespace:DXWPFApplication1"
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">
<Grid>
<view:ChildView1 x:Name="childView1"/>
</Grid>
</UserControl>
<UserControl x:Class="DXWPFApplication1.ChildView1"
xmlns:view="clr-namespace:DXWPFApplication1"
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">
<Grid>
<view:ChildView2 x:Name="childView2"/>
</Grid>
</UserControl>
ParentView背后的代码:
public ParentView()
{
InitializeComponent();
DataContext = "ViewModel"; //BreakPoint here
//
//When the first DataContext is set, all the DataContext's below are set as well
//
childView1.DataContext = DataContext;
childView1.childView2.DataContext = DataContext;
}
注意:第一次设置时的断点DataContext
为什么我只设置了 ParentView 时所有的 DataContexts 都设置了DataContext
?
我能做些什么来防止这种情况发生?