我有一个列表视图,其中包含主窗口中的一些项目。然后我添加复选框,以便在选择项目时也会检查它。
现在我尝试将所选项目从该列表视图传递到另一个列表视图,但这是我得到的:
这是我的XAML:
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#中的代码:
System.out.println(A.x);
这里出了什么问题?
答案 0 :(得分:2)
我做了一些更改,然后我意识到它不能正常工作。我有一个&#34; foreach声明&#34;循环遍历listBox1中的所有已检查/选定项目,然后在&#34; foreach语句中循环#34;有一个&#34; if语句&#34;检查listBox1中的已检查/选定项是否不在listBox2中,如果它们不是,则将它们复制到listBox2。 &#34; If语句的每个条件&#34;应该显示相关的MessageBox。现在的问题是&#34; If语句&#34;没有正常工作。我知道这一点,因为我无法看到正确的相关MessageBox到&#34; If语句&#34;的正确条件。但是这些项目不会重复,这意味着它们不会被反复复制,只是它们在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;
}
}
答案 1 :(得分:1)
问题是您要将完整列表添加为项目,这就是您获得Course cour = new Course(/*Inputs of user*/);//Your object you want to remove
List<Course> list = ...//List of your Object
Iterator<Course> iterator = list.iterator();//Convert your List to Iterable
while (iterator.hasNext()) {
Course c = iterator.next();
if(c.equals(cour)){//check with equals if equal or not
iterator.remove();//If equal remove from the list
}
}
list = Lists.newArrayList(iterator) ;
值的原因。
您可以做的是将列表中的所有选定项目循环并逐个添加
(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();
的{{1}}绑定到您的ItemsSource
lv2
SelectedItems
您可以在开头执行此操作,lv1
将始终包含lv2.ItemsSource = lv1.SelectedItems;
的所选项目,并会在所选项目更改后立即更新。