English 中文(简体)
在“ 主查看” 过滤器中设置数据 Context 设置为所有“ 儿童观察 s ”
原标题:Setting the DataContext in "MainView" filters down to all "ChildView s"

我想知道这是否是.NET的标准特征:当在 ParentView 中设置 DataContext 时,它会过滤到所有 child view

说您有父母阅读、儿童阅读1 和儿童阅读2 :

<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>

父母观点背后的代码 :

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;
        }

< 加固> NOTE: 第一次设定 Datacontext 时断点

当我只设置了“父视图” s DataContext 时,为什么所有的数据文本都设置了?

我能做些什么来防止这种情况发生?

最佳回答

这是标准行为, 通常需要。 为了防止它, 请在标记中将数据context 设置为 {x: Null} 。

问题回答

视觉树中的元素从父体中插入了数据上下文。 您的孩子视图位于父体视图的视觉树上, 这样它就可以获得父体上下文的指定。 如果您想要更改它, 您需要将它明确设置为不同内容( 无论是在子体视图构造器中还是在 xaml 中, 例如 < code\lt; view: ChildView2 DataContext="{x: Null}" x: Name= “ childView2”/ & gt; )。

为什么这样做:因为它几乎总是你想要的。

如果您对每个视图使用不同的视图模式, 您应该做的是嵌套您的视图模式 。

表示您有 < code> MainViewModel 的属性, 属性包括 ChildViewModel1 ChildViewModel2 。 然后您将儿童用户控制设置为

Datacontext="{ binding childViewModel1}" 和"{ binding childViewModel1}"

Datacontext="{ binding childViewModel2}" 分别是 < code> Datacontext="{ binding childViewModel2}"

主视图将其数据Context 保留为 MainViewModel





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签