简体   繁体   中英

Pass Selected Items from ListBox into viewmodel and delete them?

In my code I have a listbox where I select items with checkbox inside it and question is how to pass this items to viewmodel and delete them from listbox

Here is my Xaml Code with listbox

    </Border>
    <Border BorderThickness="1" BorderBrush="{DynamicResource ColumnHeaderBorder}" CornerRadius="7" Grid.Column="2" Grid.RowSpan="2" Grid.Row="0"/>
    <ListBox Grid.Row="0" Grid.Column="2" Foreground="#FFFFFF" Background="Transparent" BorderThickness="0"  Grid.RowSpan="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             ScrollViewer.VerticalScrollBarVisibility="Visible" ItemsSource="{Binding TechTabList}"  Margin="5"
            BorderBrush="{x:Null}" SelectionMode="Multiple" x:Name="TechListBox">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Focusable" Value="False"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Height="50" Width="auto">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="800"/>
                        <ColumnDefinition Width="170"/>
                    </Grid.ColumnDefinitions>
                    <Border Grid.ColumnSpan="3" BorderThickness="0,0,0,1" Margin="0,0,0,-1" BorderBrush="{DynamicResource NeonColor}" Opacity="1"/>
                    <StackPanel x:Name="StackPanel" Orientation="Horizontal" Grid.Column="0" >
                        <Viewbox Height="40" HorizontalAlignment="Center" VerticalAlignment="Center">
                            <CheckBox  Style="{StaticResource CheckBox}" Margin="0,0,3,0" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},Path=IsSelected}"/>
                        </Viewbox>
                        <TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </StackPanel>

Here is my viewmodel with Command where I need to delete selected Items

   ArchiveTable = new RelayCommand(_ArchiveTable);   
    }

    private void _ArchiveTable()
    {
        //Here foreach on selecteditems but how to pass them here 


    }

ListBox has a property SelectedItems. Bind it, as all other properties from your view model. documentation

Then you need to remove items from binded list, and refresh UI. I would do it by ObservableColletion as source for ListView. Similar problem , I like the answer which tells about using INotifyCollectionChanged

Because SelectedItems is a ReadOnly property, you can't bind directly to it.
In order to remove those items from the list one would have to use a different type of a RelayCommand . Like this:

public class RelayCommand : ICommand
{
    private Action<object> _execute;
    private Predicate<object> _canExecute;

    public RelayCommand(Action<object> execute)
    {
        _execute = execute;
        _canExecute = (o) => true;
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }

    #region Implementation of ICommand

    public bool CanExecute(object parameter)
    {
        return _canExecute.Invoke(parameter);
    }

    public void Execute(object parameter)
    {
        _execute.Invoke(parameter);
        RaiseCanExecuteChanged();
    }

    public event EventHandler CanExecuteChanged;

    public void RaiseCanExecuteChanged()
    {
        CanExecuteChanged?.Invoke(this, EventArgs.Empty);
    }

    #endregion
}  

Once you have that, then you can utilise CommandParamater for Binding. Like this:

<ListBox x:Name="TechTabList" ... />
<Button Content="Delete Selected Items" Command="{Binding ArchiveTable}" CommandParamater="{Binding ElementName=TechLabList, Path=SelectedItems}"/>  

Now in your implementation of the command:

private void _ArchiveTable(object parameter)
{
    var items = (parameter as IList).Cast<YourTypeHere>();
    //now you can remove those from the collection
    foreach(...)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM