繁体   English   中英

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

[英]Pass Selected Items from ListBox into viewmodel and delete them?

在我的代码中,我有一个列表框,其中我 select 项目里面有复选框,问题是如何将这些项目传递给 viewmodel 并从列表框中删除它们

这是我的 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>

这是我的带有命令的视图模型,我需要在其中删除选定的项目

   ArchiveTable = new RelayCommand(_ArchiveTable);   
    }

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


    }

ListBox 有一个属性 SelectedItems。 绑定它,就像您查看 model 中的所有其他属性一样。 文件

然后您需要从绑定列表中删除项目,并刷新 UI。 我会通过 ObservableColletion 作为 ListView 的源来做到这一点。 类似的问题,我喜欢关于使用 INotifyCollectionChanged 的答案

因为SelectedItemsReadOnly属性,所以不能直接绑定到它。
为了从列表中删除这些项目,必须使用不同类型的RelayCommand 像这样:

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
}  

一旦你有了它,你就可以使用CommandParamater进行绑定。 像这样:

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

现在在您执行命令时:

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