【发布时间】: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