我正在使用VS 2017将用另一种语言编写的GUI转换为WPF项目(这是新方法)。我已成功创建所有控件,并且后台代码正常工作,但listview除外。应用程序的流程是最终用户填写各种字段,填充列表视图。然后listview将所有内容导出到csv文件。以下是我设置列表视图的方法:
<ListView x:Name="lstOut" ScrollViewer.HorizontalScrollBarVisibility="Visible" HorizontalAlignment="Left" Height="205" Margin="10,394,0,0" VerticalAlignment="Top" Width="1294">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="CPU" DisplayMemberBinding="{Binding CPU}"/>
<GridViewColumn Header="RAM" DisplayMemberBinding="{Binding RAM}"/>
<GridViewColumn Header="IP Address" DisplayMemberBinding="{Binding IP}"/>
<GridViewColumn Header="Subnet" DisplayMemberBinding="{Binding Subnet}"/>
<GridViewColumn Header="Port Group" DisplayMemberBinding="{Binding PortGroup}"/>
<GridViewColumn Header="Gateway" DisplayMemberBinding="{Binding Gateway}"/>
<GridViewColumn Header="DNS" DisplayMemberBinding="{Binding DNS}"/>
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}"/>
<GridViewColumn Header="Template" DisplayMemberBinding="{Binding Template}"/>
<GridViewColumn Header="Host" DisplayMemberBinding="{Binding Host}"/>
<GridViewColumn Header="Site Code" DisplayMemberBinding="{Binding Site}"/>
<GridViewColumn Header="Folder" DisplayMemberBinding="{Binding Folder}"/>
<GridViewColumn Header="DataStore" DisplayMemberBinding="{Binding Datastore}"/>
<GridViewColumn Header="Patch Method" DisplayMemberBinding="{Binding Patch}"/>
<GridViewColumn Header="HDD1 Size" DisplayMemberBinding="{Binding HDD1Size}"/>
<GridViewColumn Header="HDD1 Format" DisplayMemberBinding="{Binding HDD1Format}"/>
<GridViewColumn Header="HDD2 Size" DisplayMemberBinding="{Binding HDD2Size}"/>
<GridViewColumn Header="HDD2 Format" DisplayMemberBinding="{Binding HDD2Format}"/>
<GridViewColumn Header="HDD3 Size" DisplayMemberBinding="{Binding HDD3Size}"/>
<GridViewColumn Header="HDD3 Format" DisplayMemberBinding="{Binding HDD3Format}"/>
<GridViewColumn Header="HDD4 Size" DisplayMemberBinding="{Binding HDD4Size}"/>
<GridViewColumn Header="HDD4 Format" DisplayMemberBinding="{Binding HDD4Format}"/>
<GridViewColumn Header="HDD5 Size" DisplayMemberBinding="{Binding HDD5Size}"/>
<GridViewColumn Header="HDD5 Format" DisplayMemberBinding="{Binding HDD5Format}"/>
</GridView>
</ListView.View>
</ListView>
然后我在项目上创建了一个类,根据用户输入的数据获取和设置列:
public class MyItem
{
public string Name { get; set; }
public int CPU { get; set; }
public int RAM { get; set; }
public string IP { get; set; }
public string Subnet { get; set; }
public string PortGroup { get; set; }
public string Gateway { get; set; }
public string DNS { get; set; }
public string Description { get; set; }
public string Template { get; set; }
public string Host { get; set; }
public string Site { get; set; }
public string Folder { get; set; }
public string Datastore { get; set; }
public string Patch { get; set; }
public int HDD1Size { get; set; }
public string HDD1Format { get; set; }
public int HDD2Size { get; set; }
public string HDD2Format { get; set; }
public int HDD3Size { get; set; }
public string HDD3Format { get; set; }
public int HDD4Size { get; set; }
public string HDD4Format { get; set; }
public int HDD5Size { get; set; }
public string HDD5Format { get; set; }
}
为了测试这一点,在主窗口初始化下我添加了以下内容:
this.lstOut.Items.Add(new MyItem {
Name = "SPMASV-KL16SQD1", CPU = 4, RAM = 8192, IP = "10.10.40.79", Subnet = "255.255.252.0", PortGroup = "Server231",
Gateway = "10.10.40.254", DNS = "10.10.40.69", Description = "Testing", Template = "OAK 2016 Template", Host = "LAX",
Site = "LAX", Folder = "Servers", Datastore = "OakStorMagic", Patch = "Patch Reboot", HDD1Size = 320, HDD1Format = "Thick",
HDD2Size = 200, HDD2Format = "Thin", HDD3Size = 60, HDD3Format = "Thick", HDD4Size = 500, HDD4Format = "Thin",
HDD5Size = 1350, HDD5Format = "EagerZeroedThick"
});
这会像我期望的那样填充listview。一旦用户单击相应的按钮,导致数据出现问题。我一直在MSDN上查看ListView类页面,以便轻松地读取单个列,并将整个内容导出为数组,但是没有任何运气。我想,我创建它的方式,我可以通过典型访问项目结构。符号(例如lstOut.Name)或将其全部转换为数组并迭代,但我没有想出一个简单的方法。
只是想找到一个正确方向的推动,如果有人有一个简单的方法来完成这个任何建议。任何帮助将不胜感激。
答案 0 :(得分:0)
您可能希望利用WPF绑定系统。请考虑从您的代码构建的以下简单示例:
XAML:
<Window x:Class="WpfTest.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:wpfTest="clr-namespace:WpfTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid Background="DimGray" Name="Grid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListView x:Name="lstOut" ScrollViewer.HorizontalScrollBarVisibility="Visible" HorizontalAlignment="Left"
VerticalAlignment="Top"
ItemsSource="{Binding MyItems}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="CPU" DisplayMemberBinding="{Binding CPU}" />
<GridViewColumn Header="RAM" DisplayMemberBinding="{Binding RAM}" />
<GridViewColumn Header="IP Address" DisplayMemberBinding="{Binding IP}" />
<GridViewColumn Header="Subnet" DisplayMemberBinding="{Binding Subnet}" />
<GridViewColumn Header="Port Group" DisplayMemberBinding="{Binding PortGroup}" />
<GridViewColumn Header="Gateway" DisplayMemberBinding="{Binding Gateway}" />
<GridViewColumn Header="DNS" DisplayMemberBinding="{Binding DNS}" />
<GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}" />
<GridViewColumn Header="Template" DisplayMemberBinding="{Binding Template}" />
<GridViewColumn Header="Host" DisplayMemberBinding="{Binding Host}" />
<GridViewColumn Header="Site Code" DisplayMemberBinding="{Binding Site}" />
<GridViewColumn Header="Folder" DisplayMemberBinding="{Binding Folder}" />
<GridViewColumn Header="DataStore" DisplayMemberBinding="{Binding Datastore}" />
<GridViewColumn Header="Patch Method" DisplayMemberBinding="{Binding Patch}" />
<GridViewColumn Header="HDD1 Size" DisplayMemberBinding="{Binding HDD1Size}" />
<GridViewColumn Header="HDD1 Format" DisplayMemberBinding="{Binding HDD1Format}" />
<GridViewColumn Header="HDD2 Size" DisplayMemberBinding="{Binding HDD2Size}" />
<GridViewColumn Header="HDD2 Format" DisplayMemberBinding="{Binding HDD2Format}" />
<GridViewColumn Header="HDD3 Size" DisplayMemberBinding="{Binding HDD3Size}" />
<GridViewColumn Header="HDD3 Format" DisplayMemberBinding="{Binding HDD3Format}" />
<GridViewColumn Header="HDD4 Size" DisplayMemberBinding="{Binding HDD4Size}" />
<GridViewColumn Header="HDD4 Format" DisplayMemberBinding="{Binding HDD4Format}" />
<GridViewColumn Header="HDD5 Size" DisplayMemberBinding="{Binding HDD5Size}" />
<GridViewColumn Header="HDD5 Format" DisplayMemberBinding="{Binding HDD5Format}" />
</GridView>
</ListView.View>
</ListView>
<Button Grid.Row="1" Content="Add new" Click="AddNew"/>
<Button Grid.Row="2" Content="Export" Click="Export"/>
</Grid>
</Window>
您可以看到窗口的数据上下文已设置为自身,您还将listview的itemsource属性绑定到一个集合,该集合应该是viewmodel中的属性(在这种情况下,因为我们正在设置datacontext到Self,viewmodel是我们的窗口本身)。然后在codebehind:
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfTest
{
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
AddNew(null, null);
}
public ObservableCollection<MyItem> MyItems { get; set; } = new ObservableCollection<MyItem>();
private void AddNew(object sender, RoutedEventArgs e)
{
MyItems.Add(new MyItem
{
Name = "SPMASV-KL16SQD1",
CPU = 4,
RAM = 8192,
IP = "10.10.40.79",
Subnet = "255.255.252.0",
PortGroup = "Server231",
Gateway = "10.10.40.254",
DNS = "10.10.40.69",
Description = "Testing",
Template = "OAK 2016 Template",
Host = "LAX",
Site = "LAX",
Folder = "Servers",
Datastore = "OakStorMagic",
Patch = "Patch Reboot",
HDD1Size = 320,
HDD1Format = "Thick",
HDD2Size = 200,
HDD2Format = "Thin",
HDD3Size = 60,
HDD3Format = "Thick",
HDD4Size = 500,
HDD4Format = "Thin",
HDD5Size = 1350,
HDD5Format = "EagerZeroedThick"
});
}
private void Export(object sender, RoutedEventArgs e)
{
foreach (var myItem in MyItems)
{
//do whatever you need to export your data...
}
}
}
}
当您单击“导出”按钮时,您可以遍历包含模型实例的集合,该集合由MyItem类表示。
答案 1 :(得分:0)
谢谢,虽然这不是我正在寻找的东西,但却让我朝着正确的方向前进。我能够弄明白我需要做什么。