WPF 将 ListView 选定的项目传递给另一个 ListView

WPF pass ListView selected items to another ListView

我有一个列表视图,主要 window 中包含一些项目。然后我添加了复选框,这样当一个项目被选中时它也会被选中。

现在我尝试将所选项目从该列表视图传递到另一个列表视图,但这是我得到的:

这是我的 XAML:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Canvas HorizontalAlignment="Left" Height="299" Margin="10,10,0,0" VerticalAlignment="Top" Width="497">
        <TabControl Height="279" Canvas.Left="10" Canvas.Top="10" Width="477">
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5">
                    <ListView Name="lv1" Margin="0,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="110" Width="471">
                        <ListViewItem>
                            <CheckBox >
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Apple"/>
                                </StackPanel>
                            </CheckBox>
                        </ListViewItem>
                        <ListViewItem>
                            <CheckBox >
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Orange"/>
                            </StackPanel>
                            </CheckBox>
                        </ListViewItem>
                    </ListView>
                    <Button Content="Copy" Width="100" Height="25" Click="Button_Click"/>
                    <ListView Name="lv2" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="110" Width="471"/>
                </Grid>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>
    </Canvas>

</Grid>

这是 c# 中的隐藏代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        List<string> lv1list = new List<string>();
        foreach (var i in lv1.SelectedItems)
        {
            lv1list.Add(i.ToString());
        }
        lv2.Items.Add(lv1.SelectedItems);
        }
    }
}

这里出了什么问题?

问题是您要添加一个完整的列表作为一个项目,这就是您获得 (Collection) 值的原因。

你可以做的是在列表循环中获取所有选定的项目,然后一个一个地添加它们

private void Button_Click(object sender, RoutedEventArgs e)
{
    var selectedItems = lv1.SelectedItems;

    for(int i = 0; i < selectedItems.Count; i++)
    {
        lv2.Items.Add(selectedItems[i]);
    }
}

您可能还想在添加新值之前清除 lv2

lv2.Items.Clear();

另一个不需要您按下按钮以使值出现在第二个列表视图中的选项是将 lv2ItemsSource 绑定到 SelectedItems你的 lv1

lv2.ItemsSource = lv1.SelectedItems;

您可以在开始时执行一次,lv2 将始终包含 lv1 的所选项目,并会在所选项目更改时立即更新。

我做了一些修改,然后我意识到它没有正常工作。我有一个 "foreach statement" 循环遍历 listBox1 中的所有 checked/selected 项,然后在 "foreach statement" 中有一个 "If statement" 来检查 listBox1 中的 checked/selected 项是否是不在 listBox2 中,如果不在,则将它们复制到 listBox2。 "If statement" 的每个条件都应显示相关的 MessageBox。现在的问题是 "If statement" 不能正常工作。我知道这一点,因为我看不到 "If statement" 的正确条件的正确相关 MessageBox。然而,这些项目没有重复,这意味着它们不会被一遍又一遍地复制,只是它们在 MessageBox 中看起来是重复的。这是我的代码,希望有人能发现我的错误

using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System;

namespace MYNAMESPACE
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>

public partial class MainWindow : INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        // To initialise the List and use it outside the class
        //MyListItem instanceOfClass = new MyListItem();
        //List<CheckBoxListItem> listOfItems = instanceOfClass.MyMethod();
        //listBox1.ItemsSource = listOfItems;
    }

    public class CheckBoxListItem : INotifyPropertyChanged
    {
        public bool CheckStatus { get; set; }
        public string Text { get; set; }
        public CheckBoxListItem(bool _CheckStatus, string _Text)
        {
            CheckStatus = _CheckStatus;
            Text = _Text;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class MyListItem
    {
        public List<CheckBoxListItem> MyMethod()
        {
            List<CheckBoxListItem> items = new List<CheckBoxListItem>();
            items.Add(new CheckBoxListItem(false, "Item 1"));
            items.Add(new CheckBoxListItem(false, "Item 2"));
            items.Add(new CheckBoxListItem(false, "Item 3"));

            return items;
        }
    }

    public List<string> selectedNames = new List<string>();
    private void CheckBox_Click(object sender, RoutedEventArgs e)
    {
        var checkBox = sender as CheckBox;
        var item = checkBox.Content;
        bool isChecked = checkBox.IsChecked.HasValue ? checkBox.IsChecked.Value : false;

        if (isChecked)
            selectedNames.Add(item.ToString());
        else
            selectedNames.Remove(item.ToString());
    }

    public string selections;
    bool updatedItems;
    public void Button_Click(object sender, RoutedEventArgs e)
    {
        foreach (string selection in selectedNames)
        {
            selections += selection + Environment.NewLine;

            if (!listBox2.Items.Contains(selection))
            {
                listBox2.Items.Add(selection);
                updatedItems = true;
            }

            else if (listBox2.Items.Contains(selection))
            {
                updatedItems = false;
            }
        }

        if (updatedItems == true)
            MessageBox.Show("Add items are: " + selections);

        else if (updatedItems == false)
            MessageBox.Show("No update to selection was made.");
    }

    private void CheckStatus(string propName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
    }
   }