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

时间:2015-12-17 21:34:55

标签: c# listview mvvm windows-runtime uwp

我已经阅读过关于命令如何在列表视图中工作的文章,所以我尝试了该代码,但是当我点击时没有任何反应。我正在使用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);
        }

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) b main
Breakpoint 1 at 0x400ae9: file vector7.cpp, line 7.
(gdb) r
Starting program: /home/dplee/work/study_room/c++/a.out 

Breakpoint 1, main () at vector7.cpp:7
7       vector<int> v(10);
(gdb) list
2   #include <vector>
3   using namespace std;
4   
5   int main()
6   {
7       vector<int> v(10);
8       int num = 0;
9   
10      for(auto& i : v)
11      {
(gdb) 

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

答案 2 :(得分:1)

尝试使用Template10.Mvvm.DelegateCommand 例如

在viewmodel中

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>