如何从列表视图中访问页面的数据上下文?

How to access a page's datacontext from inside a listview?

我正在使用 MVVM Cross 构建 UWP 应用程序。我无法将命令绑定到按钮。之前在 XAML 页面中,我使用交互来绑定其他命令。这里的问题是我们正在使用列表视图来呈现用户的 "favorite list." UnfavoriteCommand 在 ViewModel 中但永远不会被击中,因为 User.Favorite 列表在模型中并且没有 "UnfavoriteCommand."我如何使用 MVVM Cross 处理这个绑定问题?

            <ListView 
                Grid.Row="0"
                Grid.Column="0"
                CanReorderItems="True"
                CanDrag="True"  
                AllowDrop="True"
                ItemsSource="{Binding User.FavoriteList}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="500" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <StackPanel Grid.Column="0" Grid.Row="0"
                                        HorizontalAlignment="Left"
                                        Margin="0, 0, 0, 0"
                                        Height="25" >
                                    <TextBlock Text="{Binding Description, Mode=TwoWay}"
                                                   VerticalAlignment="Center"
                                                   TextAlignment="Left"/>
                                </StackPanel>
                                <StackPanel VerticalAlignment="Center" Grid.Column="1" Grid.Row="0">
                                    <Button Content="&#xf00d;" 
                                            FontFamily="{StaticResource FontAwesomeFontFamily}"                                                
                                            BorderBrush="Black">

                                        <Interactivity:Interaction.Behaviors>
                                            <Core:EventTriggerBehavior EventName="Click">
                                                <Core:EventTriggerBehavior.Actions>
                                                    <Core:InvokeCommandAction CommandParameter="{Binding}" Command="{Binding UnfavoriteCommand}"/>
                                                    <!--<Core:CallMethodAction TargetObject="{Binding}" MethodName="Unfavorite" />-->
                                                    <!--<Core:InvokeCommandAction Command="{Binding UnfavoriteCommand}" InputConverter="{StaticResource buttonConverter}"/>-->
                                                </Core:EventTriggerBehavior.Actions>
                                            </Core:EventTriggerBehavior>
                                        </Interactivity:Interaction.Behaviors>
                                    </Button>
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

此代码用于 wpf 应用程序,但我认为它与 UWP 应用程序相同,您应该使用 RelativeSource 来引用不同的源或上下文,可能您有一个 ViewModel 作为(仅限界面):

public class IAnyViewModel {
     RelayCommand UnfavoriteCommand {get;}
     ObservableCollection<UserFavorite> FavoriteList {get;set;}
}

并且此 class 被分配给页面或 window 构造函数中的控件或通过类似这样的代码:

public constructorControl(){
     this.DataContext = new AnyViewModel();
}

或 XAML.

<Page.DataContext>
 <local:AnyViewModel></local:AnyViewModel>
</Page.DataContext>

然后您可以像这样使用 RelativeSource 引用父上下文:

<Core:InvokeCommandAction 
    Command="{Binding UnfavoriteCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}">

我假设您的控件是 页面

为您的页面命名并使用 ElementName 绑定。

<Core:InvokeCommandAction CommandParameter="{Binding}" Command="{Binding DataContext.UnfavoriteCommand, ElementName=PageName}"/>