【问题标题】:How to edit the read data with StreamReader, WPF如何使用 StreamReader、WPF 编辑读取的数据
【发布时间】:2019-01-13 03:24:12
【问题描述】:

我已经使用 StreamReader 读取了一个 CSV 文件。阅读文件很容易,我有 4 列,每列都是一个字符串,但有一个(只有这一点很重要)的形式是“33 kg”,第一行是标题。我想删除 B 列中所有单元格的第一行和“kg”(其 int)。在 WindowsForms 中我可以做到这一点,但我不确定如何在 WPF 中做到这一点。

private void DataGrid_Loaded(object sender, RoutedEventArgs e)
    {
        // ... Get data.
        var patients = new List<Patient>();
        using (StreamReader reader = new StreamReader("FHDEGG.txt"))
        {
            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                patients.Add(new Patient(line));
            }
        }

        // ... Set field.
        this._list = patients;

        // ... Use ItemsSource.
        var grid = sender as DataGrid;
        grid.ItemsSource = patients;
}

【问题讨论】:

  • 请包含 csv 文件 FHDEGG.txt 的前几行
  • 请不要在得到答案后删除您的问题。问题和答案在这里供其他人查找,如果答案对您有帮助,那么它也可以帮助其他人。

标签: c# wpf streamreader


【解决方案1】:

WinForms 和 WPF 的主要区别在于绑定机制,它允许我们将 ui 逻辑与域和业务逻辑分开。阅读 MVVM 模式。

关于制动 MVVM 模式,在您的情况下,您需要为 DataGrid 列(在 xaml 中)创建自己的数据模板,然后创建将转换您的值“12 kg”=>“12”的转换器

**编辑 我忘记了第一行 - 你不能将它传递给 ItemsSource :),我如何编写创建你自己的列模板并指定标题。 看这里https://www.wpf-tutorial.com/datagrid-control/custom-columns/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多