来自DataTemplate的UWP Master Detail参考父上下文 [英] UWP Master Detail reference parent context from DataTemplate

查看:80
本文介绍了来自DataTemplate的UWP Master Detail参考父上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照以下草图,我无法在UWP Master/Details方案中实现删除"按钮.

I'm having trouble getting implementing a Remove button in a UWP Master/Details scenario as per the following sketch.

我正在使用以下XAML生成屏幕,该屏幕由ListViewContentPresenter组成,均使用DataTemplate绑定到我的视图模型.

I'm using the following XAML to generate the screen, which consists of a ListView and a ContentPresenter, both using DataTemplates to bind to my view models.

<Page.Resources>
  <DataTemplate x:Key="MasterListViewItemTemplate" x:DataType="vm:ListItemViewModel">
    <StackPanel Orientation="Horizontal">
      <SymbolIcon Width="48"
                  Height="48"
                  Symbol="Contact" />
      <TextBlock Margin="8,0"
                 VerticalAlignment="Center"
                 Text="{x:Bind Name, Mode=OneWay}" Style="{ThemeResource BaseTextBlockStyle}" />
    </StackPanel>
  </DataTemplate>
  <DataTemplate x:Key="DetailContentTemplate" x:DataType="vm:ListItemViewModel">
    <RelativePanel HorizontalAlignment="Left">
      <!-- various input controls -->

      <!-- Unable to reference command in parent context here -->
      <Button x:Name="MasterRemoveButton" Padding="12,0" Command="{Binding RemoveCommand}">Remove</button>

    </RelativePanel>
  </DataTemplate>
</Page.Resources>
...
<Grid x:Name="RoleMasterDetailGrid">
  <Grid.ColumnDefinitions>
    <ColumnDefinition x:Name="MasterColumn" Width="Auto" />
    <ColumnDefinition x:Name="DetailColumn" Width="*" />
  </Grid.ColumnDefinitions>

  <ListView x:Name="MasterListView"
            Grid.Column="0"
            IsItemClickEnabled="True"
            ItemTemplate="{StaticResource MasterListViewItemTemplate}"
            ItemsSource="{Binding ListItemViewModels}">
    <ListView.Header>
      <Button x:Name="MasterAddNewButton" Padding="12,0" Command="{Binding AddNewCommand}">
        <StackPanel Orientation="Horizontal">
          <SymbolIcon Width="48"
                      Height="48"
                      Symbol="AddFriend" />
          <TextBlock Margin="8,0"
                     VerticalAlignment="Center"
                     Text="Add" Style="{ThemeResource BaseTextBlockStyle}" />
        </StackPanel>
      </Button>
    </ListView.Header>
  </ListView>

  <ContentPresenter
      x:Name="DetailContentPresenter"
      Grid.Column="1"
      BorderThickness="1,0,0,0"
      Padding="20,16"
      BorderBrush="{ThemeResource SystemControlForegroundBaseLowBrush}"
      Content="{x:Bind MasterListView.SelectedItem, Mode=OneWay}"
      ContentTemplate="{StaticResource DetailContentTemplate}" />

</Grid>

但是,RemoveCommand命令存在于页面视图模型上,而不是ListItemViewModel,而且我似乎找不到从ContentPresenterDataTemplate引用此方法的方法.

However, the RemoveCommand command exists on the page view model, not the ListItemViewModel and I can't seem to find a way to reference this from the DataTemplate of the ContentPresenter.

请有人建议如何从DataTemplate内引用父上下文吗?

Please could someone advise how to reference the parent context from within a DataTemplate?

推荐答案

因此,可以通过直接引用您知道data context的元素来实现.只要您拥有自己的naming且不会在各个组件之间大量使用templates,它就会使用Binding ElementName并且效果很好.

So this can be achieved by directly referencing an element of which you know the data context. It uses Binding ElementName and works quite well, as long as you have your own decent naming and don't have heavy reuse of templates across components.

只需从代码中替换button声明

<Button x:Name="MasterRemoveButton" Padding="12,0" Command="{Binding RemoveCommand}">Remove</button>

使用以下代码

<Button x:Name="MasterRemoveButton" Content="Remove" Padding="12,0" Command="{Binding ElementName=DetailContentPresenter Path=DataContext.RemoveCommand}"/>

这篇关于来自DataTemplate的UWP Master Detail参考父上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆