2014-10-20 26 views
0

我正尝试使用MVVM Light向视图模型发送FlipView控件的当前项目。在Windows应用商店应用中作为RelayCommandParameter发送FlipViewItem

表示FlipView控制的XAML代码如下:

<FlipView x:Name="mainFlipView" Margin="0,10,0,10" ItemsSource="{Binding AlbumItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
    <FlipView.ItemTemplate> 
     <DataTemplate> 
      <Grid Margin="5"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
         <RowDefinition Height="*" /> 
         <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 

       <TextBlock Text="{Binding Caption}" 
         FontSize="23" 
         HorizontalAlignment="Center" 
         TextAlignment="Center" 
         TextWrapping="Wrap" 
         Margin="10"/> 

       <ScrollViewer Grid.Row="1" ZoomMode="Enabled"> 
        <uc:ImageViewer FilePath="{Binding ImagePath}" /> 
       </ScrollViewer> 

       <TextBlock Text="{Binding NrOfVotes}" FontSize="20" 
         Grid.Row="2" HorizontalAlignment="Center"       
         Margin="10" /> 
      </Grid> 
     </DataTemplate> 
    </FlipView.ItemTemplate> 
</FlipView> 
... 

含有中继命令的项目的XAML代码是:

<Page.BottomAppBar> 
    <CommandBar> 
     <AppBarButton x:Name="appBarButtonDelete" Label="Delete" Icon="Delete" 
         Command="{Binding DeleteItemCommand}" 
         CommandParameter="{Binding ElementName=mainFlipView, Path=SelectedItem}"/> 
    </CommandBar> 
</Page.BottomAppBar> 

在视图模型中,RelayCommand被声明和使用方法如下:

public class ResultsPageViewModel : ViewModelBase 
{ 
    public RelayCommand<MyModel> DeleteItemCommand { get; private set; } 

    public ResultsPageViewModel() 
    { 
     this.DeleteItemCommand = new RelayCommand<MyModel>(post => DeleteItem(post)); 
    } 

    public void DeleteItem(MyModel p) 
    { 
     //P is always null here... 
    } 
} 

问题是在DeleteItem函数我总是得到参数为null。我试过宣布RelayCommand为RelayCommand<object>,但问题依然存在。

我也尝试了“解决方法”方法来声明MyModel可绑定属性并将其绑定到FlipView。它有效,但我想知道我在这种情况下做错了什么。

预先感谢您!

+1

什么是AlbumItems的类型? – bit 2014-10-20 08:28:40

+0

这是一个'ObservableCollection ' – rhcpfan 2014-10-20 10:15:28

+0

有什么想法?谢谢! – rhcpfan 2014-10-27 07:30:09

回答

0

尝试不同的策略:在正确绑定后直接从ViewModel获取参数。

XAML

<FlipView x:Name="mainFlipView" 
      Margin="0,10,0,10" 
      ItemsSource="{Binding AlbumItems, Mode=TwoWay }" 
      SelectedItem="{Binding AlbumSelectedItem, Mode=TwoWay}"> 

视图模型

private MyModel albumSelectedItem; 
public MyModel AlbumSelectedItem 
{ 
    get 
    { 
     return albumSelectedItem; 
    } 

    set 
    { 
     if (value != null && albumSelectedItem != value) 
     { 
      albumSelectedItem = value; 
      RaisePropertyChanged(() => AlbumSelectedItem); 
     } 
    } 
} 

public void DeleteItem(MyModel p) 
{ 
    //P is always null here... 
    var pp = AlbumSelectedItem; 
} 

显然,CommandParameter是没用的。 ;-)