【问题标题】:How to access a page's datacontext from inside a listview?如何从列表视图中访问页面的数据上下文?
【发布时间】:2017-07-10 16:35:49
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: c# xaml listview uwp mvvmcross


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      此代码用于 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}}">
      

      我假设您的控制是页面

      【讨论】:

      • uwp 中没有 FindAncestor 绑定。
      • 是的,你是对的,你可以搜索“UWP 中的RelativeSource FindAncestor”,我猜Jessica 离得更近(例如:stackoverflow.com/questions/32861612/…
      猜你喜欢
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 2014-06-04
      • 1970-01-01
      相关资源
      最近更新 更多