c# - 带有按钮的 ListView 中的 UWP/MVVM 数据绑定(bind)不起作用

标签 c# listview mvvm windows-runtime uwp

我已经阅读了有关命令在 ListView 内部的工作方式有何不同的文章,因此我尝试了该代码,但是当我单击时没有任何反应。我正在使用 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:

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

      set
        {
            Set(ref selectedItem, value);
        }

最佳答案

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

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

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

关于c# - 带有按钮的 ListView 中的 UWP/MVVM 数据绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34344669/

相关文章:

c# - 如何创建一个结合了CheckedListBox和详细ListView的显示?

c# - mvvm light - 发送带回调的通知消息

c# - 将新项目添加到 CSLA EF4 模式

c# - 如何使用 LINQ 比较两个 IEnumerable 集合

c# - 具有自定义数据的ListView

c# - 如何在 wpf 的用户控件中使用命令绑定(bind)?

java - Android Firebase - 根据微调器更改来自 firebase 的 ListView 数据

java - 如何清除ListView,它只是在Android App中的旧内容之后添加新内容?

c# - 即使 DataGridView 绑定(bind)到列表,也不会显示任何记录

c# - 比较具有相同数据的两个对象