简体   繁体   English

如何将listviewitem中的按钮绑定到Winrt中的ViewModel中的Command

[英]How can i bind a button in listviewitem to a Command in ViewModel in Winrt

I have a NavigateToAccountsCommand RelayCommand property in the ViewModel. 我在ViewModel中有一个NavigateToAccountsCommand RelayCommand属性。 When I bind the same to a button on the page anywhere outside the ListView the command binding is working. 当我将相同的页面上的按钮绑定到ListView外的任何位置时,命令绑定正在运行。 However as soon as I move this to a ListView's DataTemplate its not working. 但是,只要我将其移动到ListView的DataTemplate,它就无法正常工作。

I have tried changing the binding from NavigateToAccountsCommand to DataContext.NavigateToAccountsCommand still not working. 我已经尝试将绑定从NavigateToAccountsCommand更改为DataContext.NavigateToAccountsCommand仍然无法正常工作。

Appreciate your help... 感谢您的帮助...

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

When you are inside the DataTemplate of the ListView , your data context is the current item of the ListView's ItemsSource . 当您在ListViewDataTemplate中时,您的数据上下文是ListView的ItemsSource的当前项。 As there is nothing called " NavigateToAccountsCommand " property within your AllAcounts ' each individual element, binding isn't working. 由于在AllAcounts的每个单独元素中没有任何名为“ NavigateToAccountsCommand ”的属性,因此绑定不起作用。

To fix that, you will need to reference something from the outside of DataTemplate ; 要解决这个问题,您需要从DataTemplate外部引用一些内容; following should work. 以下应该工作。 It changes the binding to reference the root Grid's DataContext which should have the property NavigateToAccountsCommand accessible. 它更改绑定以引用根网格的DataContext ,该DataContext应该可以访问属性NavigateToAccountsCommand To reference the grid, you have to add Name attribute, and then use ElementName binding. 要引用网格,必须添加Name属性,然后使用ElementName绑定。

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

You can use 您可以使用

<Button x:Name="cmdTabItemCloseButton" 
                                Style="{StaticResource TabItemCloseButtonStyle}"
                                Grid.Column="1" Margin="15,0,0,0"
                                Command="{Binding RelativeSource=
                {RelativeSource FindAncestor, 
                AncestorType={x:Type ListView}}, 
                Path=DataContext.NavigateToAccountsCommand}"
                                CommandParameter="{Binding}"/>

I had a similar problem (Win RT) which I solved by just using: 我有一个类似的问题(Win RT),我通过使用解决了:

    <GridView
        x:Name="itemGridView"
        ItemClick="ItemView_ItemClick"
        IsItemClickEnabled="True"/>

and then in the Page class: 然后在Page类中:

    private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
    {
        //e is the object being clicked
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Page.Resources 中的数据模板中将按钮绑定到 viewmodel 命令? - How can I bind a button to a viewmodel command from within a data template that's in Page.Resources? 如何将模板中的控件命令绑定到ViewModel - How can I bind a command of a control in template to my ViewModel 如何将按钮的Hold / DoubleTap事件绑定到ViewModel中的属性 - How can I bind a Hold/DoubleTap event of a button to a property in ViewModel 如何将图像按钮命令从DataTemplate选择器绑定到ViewModel? - How to bind Image Button command from a DataTemplate Selector to a ViewModel? 如何直接绑定到子视图模型? - How can I bind directly to a child viewmodel? 如何将此View绑定到此ViewModel? - How can I bind this View to this ViewModel? 生成 ListViewItem 时,如何更改此“签入”按钮的颜色? - How can I change the color of this 'CheckIn' Button when the ListViewItem is generated? 单击该项目中的按钮时,如何访问 ListviewItem 的数据? - How can I access the data of a ListviewItem when a button in that Item is clicked? 如何绑定到 ViewModel 的属性和 ViewModel 中集合项的属性? - How can I bind to properties of the ViewModel and properties of collection items in the ViewModel? 如何使用 ViewModel inheritance 绑定到子 ViewModel? - How can i bind to the sub-ViewModel using ViewModel inheritance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM