前提
C#のWPFで、テキストファイルを読み込んで特定の文字列を検索し、CSVで表にするプログラムを作っています。
文字列を検索してCSVに出力するプログラムはできましたが、DataGrid上で手入力したキーワードを参照するところが上手くいきません。
実現したいこと
- DataGrid上で手入力したデータをFileIO.csに値を渡したい。
発生している問題・エラーメッセージ
dataGrid1のItemSourceを参照し、DataGridで手入力で変更された内容を反映したいのですが、FileIO.csでnewしてWordListを参照してしまうと、新しくインスタンスを生成して参照してしまうので、初期値のデータを参照してしまいます。
該当のソースコード
MainWindow.xaml.cs
1// 2private void Button_Click_3(object sender, RoutedEventArgs e) 3 { 4 WordList wl = new WordList(); 5 wl.Show(); 6 } 7 private void Button_Click_4(object sender, RoutedEventArgs e) // エクスポート 8 { 9 // ボタン2のクリック・イベント 10 FileIO fileIo = new FileIO(); 11 12 fileIo.Export(tb.Text); 13 }
WordList.xaml
1 <Grid> 2 <Label Content="カラム名:" FontSize="12" Margin="10,29,0,192" Width="72" HorizontalAlignment="Left"/> 3 <TextBox Name="tb" Margin="67,29,115,0" Cursor="Arrow" VerticalAlignment="Top" /> 4 <DataGrid x:Name="dataGrid1" ItemsSource="{Binding DataTableView}" SelectedItem="{Binding SelectedRow}" Margin="3,68,0,116" Width ="300" Height ="200" >
WordList.xaml.cs
1using System; 2using System.Collections.ObjectModel; 3using System.Windows; 4using Microsoft.Win32; 5using System.IO; 6 7 8namespace LogCSVConverter 9{ 10 public partial class WordList : Window 11 { 12 13 public WordList() 14 { 15 16 InitializeComponent(); 17 18 var dataList = new ObservableCollection<Product>(); 19 for (int i = 0; i < 5; ++i) 20 { 21 var prefix = i + 1; 22 var data = CreateData(prefix); 23 dataList.Add(data); 24 } 25 dataGrid1.ItemsSource = dataList; 26 } 27 28 private void add_button_Click(object sender, RoutedEventArgs e) 29 { 30 var dataList = dataGrid1.ItemsSource as ObservableCollection<Product>; 31 var prefix = dataList.Count + 1; 32 var data = CreateData(prefix); 33 dataList.Add(data); 34 } 35 } 36}
Product.cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6 7namespace LogCSVConverter 8{ 9 public class Product 10 { 11 public string Keyword { get; set; } 12 public int Margin { get; set; } 13 public string Clip_char { get; set; } 14 15 public Product(string key, int margin, string clip_char) 16 { 17 Keyword = key; 18 Margin = margin; 19 Clip_char = clip_char; 20 } 21 } 22}
FileIO.cs
1using System; 2using System.IO; 3using System.Collections.ObjectModel; 4using System.Windows; 5 6using Microsoft.Win32; 7 8 9namespace LogCSVConverter 10{ 11 class FileIO 12 { 13 public void Export(string Path) 14 { 15 if (Path != string.Empty) 16 { 17 string FileContents = String.Join("\r\n", File.ReadAllLines(Path)); 18 19 // ダイアログのインスタンスを生成 20 var dialog = new SaveFileDialog(); 21 22 // ファイルの種類を設定 23 dialog.Filter = "CSVファイル (*.csv)|*.csv|全てのファイル (*.*)|*.*"; 24 25 // ダイアログを表示する 26 dialog.ShowDialog(); 27 28 if (dialog.FileName != string.Empty) //保存パスを選択してないときは通さない 29 { 30 using (StreamWriter writer = new StreamWriter(dialog.FileName)) 31 { 32 // ファイルに書き込む 33 writer.WriteLine(Search(FileContents)); 34 } 35 } 36 } 37 else 38 { 39 // 選択されたファイル名 (ファイルパス) をメッセージボックスに表示 40 MessageBox.Show("ファイルパスを指定してください"); 41 } 42 } 43 44 public string Search(string str)//検索対象のファイルの中身、検索キーワード、 抽出文字列 45 { 46 var dataList = new ObservableCollection<Product>(); 47 WordList wl = new WordList();// newしたら参照できなくなる 48 dataList = wl.ReadData();//アイテムソースのデータを読み込む 49 50 int primary_num = 0;//主キーの番号 51 52 MessageBox.Show("検索キーワード:" + dataList[primary_num] + "\n文字列の長さ:"+str.Length); 53 54 string result = ""; 55 Console.WriteLine(); 56 57 58 for (int j=0;j< 98; j++) 59 { 60 int[] pos = new int[dataList.Count]; //検索キーワードの位置([]はデータテーブルの行の数で決める) 61 string p_key = dataList[primary_num].Keyword; 62 pos[primary_num] = str.IndexOf(p_key, pos[primary_num] + 1); //主キー位置を先に検索 63 if (pos[primary_num] == -1) 64 { 65 MessageBox.Show("全ての検索処理が終了しました"); 66 break; 67 } 68 for(int i = 0; i < dataList.Count; i++) 69 { 70 if (i != primary_num) //主キー以外は主キー位置を基準に検索 71 { 72 pos[i] = str.IndexOf(dataList[i].Keyword, pos[primary_num] + 1); //現在の位置から次のキーの位置を検索 73 } 74 75 int key_length = dataList[i].Keyword.Length; 76 int clp_length = dataList[i].Clip_char.Length; 77 int margin = dataList[i].Margin; 78 result += string.Concat(str.AsSpan(pos[i] + key_length + margin, clp_length), "\t"); // 抽出したい文字列を出力する 79 } 80 result += "\n"; 81 } 82 return result; 83 } 84 } 85}
試したこと
ItemControll.ItemSourceをWordListをnewで無理やり参照したり、Staticにして参照を試みましたが、どれも上手くいきませんでした。
Staticを使わずに参照できる方法があれば、何卒ご教授お願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/27 03:24 編集
2022/04/27 08:42
2022/05/31 01:14