【问题标题】:Access parent DataContext from DataTemplate (which is User Control) [duplicate]从 DataTemplate(即用户控件)访问父 DataContext [重复]
【发布时间】:2015-07-01 16:09:11
【问题描述】:

我有一个用户控件,用作页面中的 DataTemplate。 WP8 - access datacontext of parent 中有类似问题的响应,但 DataTemplate 是在“内部”ItemsControl 中定义的。它在这样的场景中不起作用:

<Grid Name="layoutRootGrid">
        <ListView Name="listViewParent" HorizontalAlignment="Left" Height="458" Margin="42,24,0,0" VerticalAlignment="Top" Width="298" 
                  ItemsSource="{Binding ListViewSource}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <controls:ListViewControl />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>

虽然“ListViewControl”是(ListViewSource 和 ParentProp 是绑定 ViewModel 的集合和属性 - listViewParent 的 DataContext):

<Grid Background="Black" Name="templateGrid">
            <TextBlock Text="{Binding DataContext.ParentProp, ElementName=listViewParent, FallbackValue='couldnt get parent prop'}" VerticalAlignment="Top"/>
    </Grid>

【问题讨论】:

  • 你没有在任何地方使用 x:key=ListViewDataTemplate 所以你可以简单地删除它。一个小的工作示例可以显示您的问题会很好。
  • 我删除了不必要的代码,这里是示例应用程序的链接(WP8.1 通用应用程序,VS2013)s000.tinyupload.com/…

标签: xaml datatemplate win-universal-app


【解决方案1】:

通过用户控件的Tag属性传递ParentProp

MainPage.xaml:

<Grid Name="layoutRootGrid">
    <ListView Name="listViewParent"
                VerticalAlignment="Top"
                HorizontalAlignment="Left"
                Width="298"
                Height="458"
                Margin="42,24,0,0"
                ItemsSource="{Binding ListViewSource}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <controls:ListViewControl Tag="{Binding DataContext.ParentProp, ElementName=listViewParent}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

在您的用户控件中:

  • Name 属性设置为“Self”(或任何您想要的):x:Name="Self"
  • 在您的TextBlock 中,将Text 绑定到此用户控件的Tag 属性

ListViewControl.xaml:

<UserControl x:Class="Namespace.ListViewControl"
                x:Name="Self"

                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Background="Black"
            Name="templateGrid">
        <TextBlock Text="{Binding Path=Tag, ElementName=Self, FallbackValue='couldnt get parent prop'}"
                    VerticalAlignment="Top" />
    </Grid>
</UserControl>

【讨论】:

    【解决方案2】:

    您应该能够像这样绑定到父数据上下文:

    <Grid Background="Black" Name="templateGrid">
        <TextBlock Text="{Binding DataContext.ParentProp, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" />
    </Grid>
    

    【讨论】:

    • 忘了说这是WP8.1通用应用。照你说的做,我得到“AncestorType”在RelativeSource中不存在。
    • 你在 WP8.1 的 RelativeSource 枚举中得到了什么?
    • 调度程序和模式(模式值 - TemplatedParent、Self、None)
    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    相关资源
    最近更新 更多