简体   繁体   English

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

[英]WPF pass ListView selected items to another ListView

I have a list view with some items in the main window. 我在主窗口中有一些项目的列表视图。 Then I add checked boxes so that when an item is selected it also get checked. 然后,我添加了复选框,以便在选中某个项目时也将其选中。

Now I tried to pass the selected items from that list view to another list view but here is what I get: 现在,我尝试将选定的项目从该列表视图传递到另一个列表视图,但这是我得到的:

在此处输入图片说明

Here is my XAML: 这是我的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>

Here is code behind in c#: 这是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);
        }
    }
}

What went wrong here? 这里出了什么问题?

I made some more modifications and then I realized it is not working as it should. 我做了一些修改,然后我意识到它无法正常工作。 I have a "foreach statement" looping through all the checked/selected items in listBox1, then inside the "foreach statement" there is an "If statement" to check if the checked/selected items from listBox1 are not in listBox2, if they aren't then they are copied to listBox2. 我有一个“ foreach语句”循环遍历listBox1中的所有选中/选定项,然后在“ foreach语句”内有一个“ If语句”来检查listBox1中选中/选定的项是否不在listBox2中,如果不是然后将它们复制到listBox2。 Each condition of the "If statement" should display relevant MessageBox. “ If语句”的每个条件应显示相关的MessageBox。 The problem now is that the "If statement" doesn't work properly. 现在的问题是“ If语句”无法正常工作。 I know this, because I cannot see the correct relevant MessageBox to the correct condition of the "If statement". 我知道这一点,因为我看不到与“ If语句”正确条件相关的正确MessageBox。 However the items are not duplicated, meaning they are not being copied over and over only they appear duplicated in the MessageBox. 但是,这些项目不会重复,这意味着不会一遍又一遍地复制它们,而不会在MessageBox中一遍又一遍地显示它们。 Here is my code and hope someone can spot my mistake 这是我的代码,希望有人能发现我的错误

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;
    }
   }

The problem is you're adding a complete list as an item which is why you're getting the (Collection) value. 问题是您要添加完整列表作为项目,这就是为什么要获取(Collection)值的原因。

What you can do is get all the selected item in a list them loop and add them one by one 您可以做的是将所有选定的项目放入循环的列表中,并一一添加

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]);
    }
}

You might also want to clear your lv2 before adding the new values 您可能还想在添加新值之前清除lv2

lv2.Items.Clear();

Another option which wouldn't require you to press a button so that the values appear in the second listview would be to bind the ItemsSource of your lv2 to the SelectedItems of your lv1 这不会需要你按下一个按钮,以使值出现在第二列表视图另一个选择是到绑定ItemsSource您的lv2SelectedItems您的lv1

lv2.ItemsSource = lv1.SelectedItems;

You can do that once at the beginning and lv2 will always contain the selected items of lv1 and will update as soon as the selected items changes. 您可以在开始时执行一次,并且lv2将始终包含lv1的选定项,并且一旦选定项发生更改,它将立即更新。

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

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