带有按钮的列表视图中的UWP/MVVM数据绑定不起作用

本文关键字:MVVM 数据绑定 不起作用 UWP 视图 按钮 列表 | 更新日期: 2023-09-27 18:27:18

我读过关于命令在列表视图中如何不同工作的文章,所以我尝试了该代码,但当我单击时,什么也没发生。我正在使用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);
        }

带有按钮的列表视图中的UWP/MVVM数据绑定不起作用

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

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

因为根对象不知道RunCommand本身,但DataContext(vm)是.

<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.DegateCommand例如

在视图模型中

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>