【问题标题】:WPF pass ListView selected items to another ListViewWPF 将 ListView 选定项传递给另一个 ListView
【发布时间】:2017-09-21 16:52:45
【问题描述】:

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

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

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

这里出了什么问题?

【问题讨论】:

  • 我是否在图像中突出显示了正确的内容?我想这就是你正在研究的问题,对吧?
  • 是的,你做到了。抱歉,我做不到,感谢您的编辑:)

标签: c# wpf listview selecteditem


【解决方案1】:

问题是您将完整列表添加为一个项目,这就是您获得 (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 绑定到您的lv1SelectedItems

lv2.ItemsSource = lv1.SelectedItems;

您可以在开始时这样做一次,lv2 将始终包含 lv1 的选定项,并且会在选定项更改时立即更新。

【讨论】:

  • 感谢您的回答。也许我的 XMAL 不正确,但按钮现在没有复制任何内容,当我在将项目添加到 lv2 的行处放置断点时,我得到了 PresentationFramework.dll 中发生的“System.InvalidOperationException”类型的未处理异常附加信息:元素已经有一个逻辑父级。它必须先与旧父级分离,然后才能附加到新父级。
【解决方案2】:

我做了更多修改,然后我意识到它没有按应有的方式工作。我有一个“foreach 语句”循环遍历 listBox1 中的所有选中/选中的项目,然后在“foreach 语句”中有一个“If 语句”来检查 listBox1 中的选中/选中的项目是否不在 listBox2 中,如果它们不在't 然后它们被复制到 listBox2。 “If 语句”的每个条件都应显示相关的 MessageBox。现在的问题是“If 语句”不能正常工作。我知道这一点,因为我看不到与“If 语句”的正确条件相关的正确 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;
    }
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 2019-07-10
    • 2020-05-12
    • 2012-08-30
    相关资源
    最近更新 更多