UWP/MVVM 使用按钮在列表视图中进行数据绑定不起作用

UWP/MVVM data binding in listview with a button does not work

我已经阅读了有关命令在列表视图内部的工作方式有何不同的文章,因此我尝试了该代码,但是当我单击时没有任何反应。我正在使用 Template10。我发现的大多数示例都是针对具有不兼容部分的 WPF 的。只需要最低限度的点击按钮来调用下面的方法。我的代码的相关部分是:

  <ListView x:Name="lvMain"
                      ItemsSource="{Binding LeadSpeakerItems}"
                      SelectedItem="{Binding Lsi}">
                <ListView.ItemTemplate>

...

       <Button Content="Details"
                                    Command="{Binding ElementName=Root, Path=RunCommand}"
                                    Grid.Column="1" />
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>

代码:

        public ICommand RunCommand { get; private set; }
    public MainPageViewModel()
    {
        if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
        {
            LeadSpeakerItems.Add(new LeadSpeakerItem {
                VelocifyLeadTitle = "The is the lead title that says somrthing about something and her a number 234-456-3454",
                VelocifyFirstName = "BobbiMinajobi",
                VelocifyLastName = "Luciferdissikusliskus",
                VelocifyLoanAmount = 254000.00,
                VelocifyHomeValue = 278000.00
            });
        }

        RunCommand = new DelegateCommand<object>(OnRunCommand, CanRunCommand);
    }

      private void OnRunCommand(object obj)
    {
        // use the SelectedCustomer object here...
    }

    private bool CanRunCommand(object obj)
    {
        return true;
    } 

编辑 1:

当按钮或列表视图项目被选中时,我如何获得该特定项目?当发生这种情况时,我正在尝试获取这段代码 运行 。我错过了一些东西。

      set
        {
            Set(ref selectedItem, value);
        }

假设 Root 是您的页面或另一个视图模型为 DataContext 的控件,您应该将 XAML 更改为:

<Button Content="Details"
        Command="{Binding ElementName=Root, Path=DataContext.RunCommand}"
        Grid.Column="1" />

因为 RunCommand 本身不为您的 Root 对象所知,但 DataContext(您的虚拟机)为。

<Button Content="Details"
                        Command="{Binding RunCommand}"
                        Grid.Column="1" />

<ListView 
     x:Name="lvMain"
     DataContext={Binding}>
     ....
</ListView>

<Button 
    DataContext="{Binding ElementName=lvMain, Path=DataContext}"
    Content="Details"
    Command="{Binding RunCommand}"
    Grid.Column="1" />

尝试使用 Template10.Mvvm.DelegateCommand 例如

在视图模型中

public ICommand ItemSelected
        {
            get
            {
                return new Template10.Mvvm.DelegateCommand<string>((s) =>
                 {
                     NavigationService.Navigate(typeof(DetailPage), s);

                 });
            }

        }

添加到您的页面

<page
xmlns:Behaviors="using:Template10.Behaviors"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:vm="using:....ViewModel"
....>
<Page.DataContext>
    <vm:ViewModel />
</Page.DataContext>

在您的列表视图中

    <ListView x:Name="listView" ... ItemsSource="{x:Bind ViewModel.ListItem}" >
                   <Interactivity:Interaction.Behaviors>
                    <Core:EventTriggerBehavior EventName="Tapped">
                        <Core:InvokeCommandAction     Command="{x:Bind ViewModel.ItemSelected}"     CommandParameter="{Binding ElementName=listView,Path=SelectedItem}"/>
                    </Core:EventTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
</ListView>