如何将 listviewitem 中的按钮绑定到 Winrt 中 ViewModel 中的命令 [英] How can i bind a button in listviewitem to a Command in ViewModel in Winrt

查看:44
本文介绍了如何将 listviewitem 中的按钮绑定到 Winrt 中 ViewModel 中的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ViewModel 中有一个 NavigateToAccountsCommand RelayCommand 属性.当我将它绑定到 ListView 之外的页面上的按钮时,命令绑定正在工作.但是,一旦我将其移至 ListView 的 DataTemplate,它就无法正常工作.

I have a NavigateToAccountsCommand RelayCommand property in the ViewModel. When I bind the same to a button on the page anywhere outside the ListView the command binding is working. However as soon as I move this to a ListView's DataTemplate its not working.

我尝试将绑定从 NavigateToAccountsCommand 更改为 DataContext.NavigateToAccountsCommand 仍然无效.

I have tried changing the binding from NavigateToAccountsCommand to DataContext.NavigateToAccountsCommand still not working.

感谢您的帮助...

<Page
x:Class="FinancePRO.App.Views.AccountsView"
DataContext="{Binding AccountsViewModel, Source={StaticResource MainViewModelLocator}}"
mc:Ignorable="d">

<Grid>
      <!--**This one is working**-->
      <Button  Command="{Binding NavigateToAccountsCommand}" >

     <!--**This one is not working**-->
        <ListView ItemsSource="{Binding AllAccounts}" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel HorizontalAlignment="Stretch">
                      <TextBlock Text="{Binding AccountName}"/>
                      <Button  Command="{Binding NavigateToAccountsCommand}">
                                </Button>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

推荐答案

当您在 ListViewDataTemplate 中时,您的数据上下文是ListView 的 ItemsSource.由于您的 AllAcounts' 每个单独元素中都没有名为NavigateToAccountsCommand"的属性,因此绑定不起作用.

When you are inside the DataTemplate of the ListView, your data context is the current item of the ListView's ItemsSource. As there is nothing called "NavigateToAccountsCommand" property within your AllAcounts' each individual element, binding isn't working.

要解决这个问题,您需要从 DataTemplate 外部引用一些内容;以下应该工作.它更改绑定以引用根网格的 DataContext,它应该具有可访问的属性 NavigateToAccountsCommand.要引用网格,您必须添加 Name 属性,然后使用 ElementName 绑定.

To fix that, you will need to reference something from the outside of DataTemplate; following should work. It changes the binding to reference the root Grid's DataContext which should have the property NavigateToAccountsCommand accessible. To reference the grid, you have to add Name attribute, and then use ElementName binding.

    <Grid Name="Root">
          <!--**This one is working**-->
          <Button  Command="{Binding NavigateToAccountsCommand}" >

         <!--**This one is not working**-->
            <ListView ItemsSource="{Binding AllAccounts}" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel HorizontalAlignment="Stretch">
                          <TextBlock Text="{Binding AccountName}"/>
                          <Button  Command"{Binding ElementName=Root, Path=DataContext.NavigateToAccountsCommand}">
                                    </Button>
                    </DataTemplate>
                </ListView.ItemTemplate>
        </ListView>
   </Grid>

这篇关于如何将 listviewitem 中的按钮绑定到 Winrt 中 ViewModel 中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆