UWP Master Detail 从 DataTemplate 引用父上下文
UWP Master Detail reference parent context from DataTemplate
我无法按照以下草图在 UWP Master/Details 场景中实现删除按钮。
我正在使用以下 XAML 生成屏幕,它由一个 ListView
和一个 ContentPresenter
组成,都使用 DataTemplate
绑定到我的查看模型。
<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
,我似乎无法从 DataTemplate
中找到引用它的方法ContentPresenter
.
有人可以建议如何从 DataTemplate
中引用父上下文吗?
因此,这可以通过直接引用您知道 data context
的元素来实现。它使用 Binding ElementName
并且工作得很好,只要你有自己的体面 naming
并且没有跨组件大量重用 templates
。
只需替换代码中的 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}"/>
我无法按照以下草图在 UWP Master/Details 场景中实现删除按钮。
我正在使用以下 XAML 生成屏幕,它由一个 ListView
和一个 ContentPresenter
组成,都使用 DataTemplate
绑定到我的查看模型。
<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
,我似乎无法从 DataTemplate
中找到引用它的方法ContentPresenter
.
有人可以建议如何从 DataTemplate
中引用父上下文吗?
因此,这可以通过直接引用您知道 data context
的元素来实现。它使用 Binding ElementName
并且工作得很好,只要你有自己的体面 naming
并且没有跨组件大量重用 templates
。
只需替换代码中的 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}"/>