2016-03-27 9 views
1

こんにちは私はすべての行が1つのテキストボックスと1つのボタンを含む1つのリストボックスを持っています。ListBoxのSelectedItemをバインドする

ボタンをクリックするとその行がリストボックスから削除されます。この作業はmvvmパターンで行います

私はこのためにコマンドを使用します。

これは私のXAMLです:

<DataTemplate x:Key="CategoryTemplate"> 
    <Border Width="400" Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="4"> 

     <StackPanel Grid.Row="0" Orientation="Horizontal"> 
      <TextBlock Width="300" Margin="5" Text="{Binding Path=Name}"></TextBlock> 
      <Button Name="btnDeleteCategory" Width="50" Margin="5" 
       Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType=ListBox}}" 
       CommandParameter="{Binding}" Content="-" /> 
     </StackPanel> 

    </Border> 
</DataTemplate> 
<ListBox Grid.Column="0" Grid.Row="0" Name="lstCategory" 
     ItemTemplate="{StaticResource CategoryTemplate}" 
     ItemsSource="{Binding Path=GetAllCategories}"> 

</ListBox> 

とのviewmodelクラスで私は、このコマンドを持っている:GetAllCategoriesはobservecollectionのpropertある

private ObjectButtonCommand<Category> _deleteCommand; 
public ObjectButtonCommand<Category> DeleteCommand 
{ 
    get 
    { 
     return _deleteCommand 
      ?? (_deleteCommand = new ObjectButtonCommand<Category>(
      _category => 
      { 
       GetAllCategories.Remove(_category); 

      })); 
    } 
} 

、これが私のObjectButtonCommandコードです:

public class ObjectButtonCommand<T> : ICommand 
    where T:class 
{ 
    private Action<T> WhatToExecute; 

    public ObjectButtonCommand(Action<T> What) 
    { 
     WhatToExecute = What; 
    } 

    public event EventHandler CanExecuteChanged; 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public void Execute(object parameter) 
    { 
     WhatToExecute((T)parameter); 
    } 
} 

は今、すべてのものはOKで、行が削除クリックボタン。

今私が欲しい、このプロセスを繰り返している私は、リストボックス

の1行を選択したとき、私はこのコードを試してみてください。

<ListBox Grid.Column="0" Grid.Row="0" Name="lstCategory" ItemTemplate="{StaticResource CategoryTemplate}" ItemsSource="{Binding Path=GetAllCategories}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="SelectionChanged"> 
      <i:InvokeCommandAction Command="{Binding DataContext.DeleteCommand , RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding Path=SelectedItems,ElementName=lstCategory}"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</ListBox> 

をしかし、私はこのコードでは、このエラーを取得:WhatToExecute((T)パラメータ) ; { "と入力することができませんでし型のオブジェクトをキャストする 'System.Windows.Controls.SelectedItemCollection' 'Sepand.WPFProject.Model.Model.Category'。"}

私は何をすべき?

+1

'_deleteCommand'は' readonly'でなければなりません。また、次回は質問を正しくフォーマットしてください。 –

答えて

1

あなたがリストに(DataTemplateから渡された)個々の項目を包むない限り、したがって、あなたは両方のケースで同じコマンドを使用することはできません、リストで削除コマンドを選択を渡します最初。

をパラメーター(タイプListBox.SelectedItems)とし、項目をCategoryにキャストして個別に削除する新しいコマンドを定義する必要があります。

あなたはちょうどあなたがSelectedItemへの結合を変更する必要があり、単一の選択した項目を削除すると、既存のコマンドにnullであるSelectedItemのケースを処理することが必要であれば。例えばそれがInvokeCommandActionによって尊重される場合、CanExecuteparameter != nullに変更してください。

0

こんにちはムハンマドは、あなたはこれを試すことができます。

をごListboxために、あなたのSelectionModeSingleに設定されていることを確認してください。次にSelectedItemsの代わりにCommandParameterSelectedItemを参照するように変更してください。これと同じように:

   <ListBox Grid.Column="0" Grid.Row="0" Name="lstCategory" ItemTemplate="{StaticResource CategoryTemplate}" ItemsSource="{Binding Path=GetAllCategories}" SelectionMode="Single"> 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="SelectionChanged"> 
         <i:InvokeCommandAction Command="{Binding DataContext.DeleteCommand , RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter="{Binding Path=SelectedItem,ElementName=lstCategory}"/> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 

      </ListBox> 
+0

このエラーが発生しました{"値はnullにはできません。\ r \ nパラメータ名:エンティティ "} –

+0

は、スタックトレースと行番号を注意深く見てください。その点までコードに戻り、何がnullであるかを調べてください。コードと完全なスタックトレースは不可能ですので、ここではトラブルシューティングが不可能です。質問を変更してより多くのコードを追加し、可能な限り明確にし、より多くの人々が試して助けてくれるようにしてください。 – Taterhead

関連する問題