我刚刚制作了一个C#控制台应用程序,它可以读取.csv文件并以整洁的方式将其写入控制台,但是现在我想让它进入WPF使它更加整洁。将.csv文件读入WPF应用程序(C#)
我以前的控制台应用程序是这样的:
class Program
{
static void Main(string[] args)
{
string[] tokens;
char[] separators = { ';' };
string str = "";
FileStream fs = new FileStream(@"D:\Dokumenter\Skole\6. semester\GUI\Exercises\Exercise2\02 deltagerliste.csv",
FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
while ((str = sr.ReadLine()) != null)
{
tokens = str.Split(separators, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(String.Format("{0,-20}", tokens[0]) +
String.Format("{0,-15}", tokens[1]) +
String.Format("{0,-15}", tokens[2]) +
String.Format("{0,-15}", tokens[3]));
}
Console.ReadLine();
}
}
它的伟大工程,但我必须承认,我有困难找到从哪里开始与WPF应用程序。
到目前为止,我已经想出了以下带有.csv文件的四个头文件的XAML代码(因为它有四列),我假设我必须找到一种方法将相应的行放入相应的列中。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Deltagerliste" Height="350" Width="525" WindowStartupLocation="CenterScreen" WindowState="Maximized"
Background="DeepPink"
>
<ListView HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="517">
<ListView.View>
<GridView>
<GridViewColumn Header="First name"/>
<GridViewColumn Header="Last name"/>
<GridViewColumn Header="ID"/>
<GridViewColumn Header="Email"/>
</GridView>
</ListView.View>
</ListView>
我的主要和最初的问题是我的文件到ListView如何阅读。我对C#和XAML都很陌生,尽管我很清楚如何在C#中打开和读取文件,但XAML中的语法对我来说有点困惑。
你可能想阅读一些有关MVVM模式,数据绑定等 –
您可以优化的String.Format代码:的String.Format(“{0,} -20 {1,-15} {2,-15} {3,-15}“,令牌[0],令牌[1],令牌[2],令牌[3])所以你不必连接4个字符串.formats – Measuring