UWP-如何将ContentPresenter的元素绑定到主ViewModel? [英] UWP - How to bind a ContentPresenter's element to the main ViewModel?
问题描述
我在一个应用程序上工作,用户可以在其中编辑大"表格:
I work on an app where the user can edit a "big" form:
- 该表单包含许多字段:在 Pivot 中显示,其中每个 PivotItem 代表表单的类别.
- 某些类别包含一个或多个子项:它们通过主从细节演示文稿显示.
- The form contains a lot of fields: it is displayed in a Pivot, where each PivotItem represents a category of the form.
- Some categories contain one or more child items: they are displayed through a Master-Detail presentation.
要显示详细信息"部分的项目,我使用直接绑定到模型的 ContentPresenter :
To display the items of the Detail part, I use a ContentPresenter that is binded directly to the Model:
<ContentPresenter
x:Name="DetailContentPresenter"
Grid.Column="1"
Grid.RowSpan="2"
BorderThickness="1,0,0,0"
Padding="24,0"
BorderBrush="{ThemeResource SystemControlForegroundBaseLowBrush}"
Content="{x:Bind MasterListView.SelectedItem, Mode=OneWay}">
<ContentPresenter.ContentTemplate>
<DataTemplate x:DataType="models:Form_Parts">
<...>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
这很好用,但是在主窗体中有一个第三级":我通过 GridView 在ContentPresenter中显示的照片列表.这也很好,但是我想使用添加"按钮或"ItemClick"事件与照片进行交互:我看不到如何在主ViewModel中绑定Command,而不是ContentPresenter的DataContext.这种形式的其他字段在此ViewModel上绑定得很好.
This works fine, but there is a "third" level in the main form: a list of photos, that I display in the ContentPresenter through a GridView. This works also great, but I would like to use an "Add" button or the "ItemClick" event to interact with the photos: I don't see how I could bind a Command in the main ViewModel, instead of the ContentPresenter's DataContext. The other fields of this form bind well on this ViewModel.
我做了一些测试但没有任何结果:
I did some tests without any result:
<StackPanel Orientation="Horizontal" Grid.Row="1">
<GridView ItemsSource="{Binding images}"
ItemClick="{Binding PhotoClickCommand}">
<GridView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Gray" BorderThickness="1"
Padding="10"
Height="150" Width="190">
<Image Stretch="UniformToFill"
Source="{Binding bitmap_image}" />
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<Border BorderBrush="Gray" BorderThickness="1"
Padding="10"
Height="150" Width="190">
<Button Command="{Binding Path=DataContext.AddPhotoCommand, ElementName=DetailsPage}"
Height="100" Width="100">
<Viewbox>
<SymbolIcon Symbol="Add"/>
</Viewbox>
</Button>
</Border>
</StackPanel>
=>有没有办法做到这一点?
=> is there a way to doing this?
我用添加"按钮解决了我的问题,谢谢Alteik:
Edit : I solved my problem for the "Add" button thanks Alteik with:
<Button Command="{Binding ElementName=Root, Path=DataContext.AddPhotoCommand}" Height="100" Width="100">
但是我总是遇到 GridView 的 ItemClick 问题.我试过了:
But I always get a problem with the ItemClick of the GridView. I tried this:
<GridView ItemsSource="{Binding images}"
IsItemClickEnabled="True"
SelectionMode="None">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding ElementName=Root, Path=DataContext.EditPhotoCommand}"
CommandParameter="{Binding Mode=OneWay}"/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
<...>
</GridView>
但是我在ViewModel中没有得到预期的参数:
But I don't get the expected parameter in the ViewModel:
private void EditPhoto(object sender)
{
Images sImage = (Images)sender;
if (sImage != null)
{
NavigationService.NavigateTo<CommentImageViewModel>(this, new Object[] { sImage }, true);
}
}
发件人是当前的 Form_Parts (ContentPresenter的DataTemplate),而我将获取图像集合中的选定项,并将其绑定到GridView ...
The sender is the current Form_Parts (the DataTemplate of the ContentPresenter), whereas I would get the selected item of my images collection, that is binded to the GridView...
=>我该怎么做才能解决此问题?
=> Whad could I do to fix this?
推荐答案
您应该能够在子元素中执行以下操作来更改DataContext:
You should be able to change the DataContext in a child element doing this:
Command"{Binding ElementName=Root, Path=DataContext.TheCommandFromRoot}"
显然,您需要将父元素的名称设置为"Root"
Obviously you need to set the name of the parent's element to "Root"
这篇关于UWP-如何将ContentPresenter的元素绑定到主ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!