【问题标题】:How can i bind a button in listviewitem to a Command in ViewModel in Winrt如何将 listviewitem 中的按钮绑定到 Winrt 中 ViewModel 中的命令
【发布时间】:2013-10-28 22:45:47
【问题描述】:

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

我已尝试将绑定从 NavigateToAccountsCommand 更改为 DataContext.NavigateToAccountsCommand 仍然无法正常工作。

感谢您的帮助...

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

【问题讨论】:

    标签: c# mvvm winrt-xaml


    【解决方案1】:

    你可以使用

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

    【讨论】:

    • 感谢 Dhaval 的及时回复。这适用于普通的 WPF 应用程序,但不适用于 WInRT 应用程序。无论如何,谢谢。
    【解决方案2】:

    当您在ListViewDataTemplate 内时,您的数据上下文是ListView 的ItemsSource 的当前项。由于您的 AllAcounts' 每个单独元素中没有任何名为“NavigateToAccountsCommand”的属性,因此绑定不起作用。

    要解决这个问题,您需要从DataTemplate 外部引用一些内容;以下应该工作。它将绑定更改为引用根网格的DataContext,它应该具有属性NavigateToAccountsCommand 可访问。要引用网格,您必须添加 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>
    

    【讨论】:

    • 感谢 loopedcode 的及时回复。这解决了我的问题。
    【解决方案3】:

    我有一个类似的问题(Win RT),我通过使用解决了这个问题:

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

    然后在Page类中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 2020-05-28
      • 2020-07-12
      • 2012-09-03
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多