简体   繁体   English

将 ListBox 中的选定项目传递到视图模型并删除它们?

[英]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在我的代码中,我有一个列表框,其中我 select 项目里面有复选框,问题是如何将这些项目传递给 viewmodel 并从列表框中删除它们

Here is my Xaml Code with listbox这是我的 Xaml 代码与列表框

    </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. ListBox 有一个属性 SelectedItems。 Bind it, as all other properties from your view model.绑定它,就像您查看 model 中的所有其他属性一样。 documentation 文件

Then you need to remove items from binded list, and refresh UI.然后您需要从绑定列表中删除项目,并刷新 UI。 I would do it by ObservableColletion as source for ListView.我会通过 ObservableColletion 作为 ListView 的源来做到这一点。 Similar problem , I like the answer which tells about using INotifyCollectionChanged 类似的问题,我喜欢关于使用 INotifyCollectionChanged 的答案

Because SelectedItems is a ReadOnly property, you can't bind directly to it.因为SelectedItemsReadOnly属性,所以不能直接绑定到它。
In order to remove those items from the list one would have to use a different type of a RelayCommand .为了从列表中删除这些项目,必须使用不同类型的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.一旦你有了它,你就可以使用CommandParamater进行绑定。 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(...)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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