簡體   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