Wpf 绑定到子视图中的datacontext

Wpf 绑定到子视图中的datacontext,wpf,datatemplate,Wpf,Datatemplate,我有一个tabcontrol的datatemplate,绑定到视图列表。 我希望用BusyIndicator包装每个视图,并将BusyIndicator的IsBusy属性绑定到views IsBusy属性。 这是模板: <TabControl ItemsSource="{Binding ViewModels}" x:Name="TabControlViews"> <TabControl.ItemTemplate> <!-- this is

我有一个tabcontrol的datatemplate,绑定到视图列表。 我希望用BusyIndicator包装每个视图,并将BusyIndicator的IsBusy属性绑定到views IsBusy属性。 这是模板:

 <TabControl ItemsSource="{Binding ViewModels}"  x:Name="TabControlViews">
    <TabControl.ItemTemplate>
        <!-- this is the header template-->
        <DataTemplate>
            <TextBlock
                    Text="{Binding Title}" />
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <!-- this is the body of the TabItem template-->
        <DataTemplate>
            <xctk:BusyIndicator IsBusy="{Binding DataContext.IsBusy, RelativeSource={RelativeSource FindAncestor,AncestorType=ContentControl}}">
                <xctk:BusyIndicator.BusyContent>
                    <TextBlock Text="Vent venligst ..." VerticalAlignment="Center" />
                </xctk:BusyIndicator.BusyContent>
                <ContentControl Content="{Binding View}"/>
            </xctk:BusyIndicator>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>
因此,我要绑定到的属性的路径是: ContentControl.DataContext.View.DataContext.Isbusy


可能吗?

你为什么要这样做?这是WPF。。。我们处理的是数据,而不是UI控件。只需将您的
IsBusy
属性添加到它所属的视图模型中。视图模型的唯一任务是提供视图中所需的所有数据。如果您的要求是在忙时显示忙指示器,则视图模型中需要该属性

这就像其他数据一样。。。你需要展示一个人吗?。。将其放在视图模型中。。。您需要显示一个地址?。。将其放在视图模型中。。。您需要显示忙指示灯?。。将其放在视图模型中

此外,如果将
IsBusy
属性放在视图模型中,则无需询问如何将其绑定到UI,因为它与绑定任何其他属性一样简单,而
BusyIndicator
控件只是视图的一部分:

<xctk:BusyIndicator IsBusy="{Binding IsBusy}" />

唯一的问题是
绑定中指定的视图必须是定义
绑定的视图的父视图或父视图。

这是有效的,因为数据模板的datacontext是TabViewModel。因此,我们需要绑定到View属性的datacontext以访问IsBusy属性

<xctk:BusyIndicator IsBusy="{Binding View.DataContext.IsBusy}">


它位于viewmodel上,但具有IsBusy属性的viewmodel位于选项卡viewmodel.view上加载的视图上。我编写的代码是我自己对棱镜区域的替换,以便在加载视图时有更多的控制权。
{Binding DataContext.IsBusy, RelativeSource={RelativeSource AncestorType={x:Type 
YourXmlNamespacePrefix:YourView}}}
<xctk:BusyIndicator IsBusy="{Binding View.DataContext.IsBusy}">