如何从ListView内部访问页面的DataContext?

3

我正在使用MVVM Cross构建UWP应用程序。 我在绑定命令到按钮时遇到了问题。 以前在XAML页面中,我使用交互性来绑定其他命令。 这里的问题是,我们使用列表视图来呈现用户的“收藏夹”。 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>
2个回答

2
给你的页面命名并使用ElementName绑定。
<Core:InvokeCommandAction CommandParameter="{Binding}" Command="{Binding DataContext.UnfavoriteCommand, ElementName=PageName}"/>

0

这段代码适用于WPF应用程序,但我认为在UWP应用程序中使用相同的方式。您应该使用RelativeSource来引用不同的源或上下文,也许您有一个ViewModel(仅接口):

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

而这个类在构造函数或代码中被分配给页面或窗口控件,类似于以下内容:

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}}">

我假设你的控件是Page


UWP 中没有 FindAncestor 绑定。 - Justin XL
是的,你说得对,你可以搜索“UWP中的RelativeSource FindAncestor”。我猜Jessica更接近,例如:https://dev59.com/wY_ea4cB1Zd3GeqPQp-0。 - Juan Pablo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接