2017-05-21 5 views
0
<ScrollViewer VerticalScrollBarVisibility="Auto"> 
     <ListBox x:Name="RootListView" ItemsSource="{Binding Ratings}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid ShowGridLines="True"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="auto" /> 
          <RowDefinition Height="auto" /> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="40" /> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition /> 
          <ColumnDefinition /> 
         </Grid.ColumnDefinitions>  

         .... Some TextBoxes with Labels are here to fill the grid ..... 

         <Button Grid.Row="4" Grid.Column="1" Content="Delete" 
           Command="{Binding ElementName=RootListView, Path=DataContext.DeleteRatingCommand}" 
           CommandParameter="{Binding ElementName=RootListView, Path=SelectedItem}"> 

         </Button> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
</ScrollViewer> 

どこRatingsは次のとおりです。WPFのSelectedItemは、コマンド・パラメータとして渡された私の見解モデルの値を返さない

public ObservableCollection<RatingModel> Ratings; 

そしてDeleteRatingCommandICommandオブジェクトです。

このビューに表示されているレーティング(情報を含むグリッド)はすべて、削除ボタンがあります。対応する評価のボタンが押されると、SelectedItemプロパティを持つビューモデルに評価オブジェクトを送信したいと思います。バインドされたメソッドが呼び出されたにもかかわらず、SelectedItemパラメーター、つまりnullの値を渡したものはありません。この場合、SelectedItemを正しく使用して、評価を削除するビューモデルに情報を送信するにはどうすればよいですか?他の可能性はありますか?このアプローチが正しいかどうかはわかりません。

答えて

1

ボタンをクリックしても実際にSelectedItemプロパティが設定されていないと仮定します。

バインドを行コンテキストデータに変更してみてください。

CommandParameter="{Binding Path=.}" 
+0

これは私が思いついたものです。あなたはそれを 'CommandParameter =" {Binding} "' – yoger

関連する問題