2015-12-17 65 views
1

我已閱讀關於如何命令工作不同內部的列表視圖的文章,所以我嘗試了代碼,但是當我點擊什麼都沒有發生。我正在使用Template10。我發現的大多數例子都是針對WPF,它們具有不兼容的部分。只需要最低限度的點擊按鈕來調用下面的方法。我的代碼的相關部分:UWP/MVVM數據綁定在一個按鈕的列表視圖不起作用

<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

假設根是你的網頁或與您的視圖模型爲DataContext另一個控制,你應該改變你的XAML到:

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

RunCommand本身是不知道你的根對象,但DataContext(您vm)是。

+0

它的作品!爲了這個簡單的改變花了我幾個小時。更令人困惑的是,當我自己搜索答案時,WPF或其他版本與UWP不兼容的答案有些令人困惑 – punkouter

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

嘗試使用視圖模型

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

       }); 
      } 

     } 

Template10.Mvvm.DelegateCommand 例如

添加到您的網頁

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