【问题标题】:WP8 - access datacontext of parentWP8 - 访问父级的数据上下文
【发布时间】:2013-04-08 20:39:57
【问题描述】:

如何在 windows phone 8 中访问父元素的数据上下文? AncestorType 在 WP8 中不可用。

<ItemsControl x:Name="Elements" ItemsSource="{Binding MyList}" Grid.Row="2" Grid.Column="3">
<ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <toolkit:WrapPanel  />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="e"  Width="100" Height="100" Command="{Binding MyCommand" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

“MyCommand”在“MyList”之外定义。那么如何从我的按钮访问根数据上下文(DataContext = MyClass)。 MyCommand 在 MyClass 类中定义。

提前致谢。

【问题讨论】:

    标签: xaml binding windows-phone-8 commandbinding


    【解决方案1】:

    您可以改用 ElementName 绑定。如果您的根网格(直接在您的页面内)称为 LayoutRoot:

    <Button Command="{Binding DataContext.MyCommand, ElementName=LayoutRoot}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-29
      • 2014-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-10
      相关资源
      最近更新 更多