前提・実現したいこと
下記のコードでは、DataGridに表示、ボタン押下で再表示をしています。
(DataGridの値を変更しても、元に戻る)
これを修正して、DataGridの値の変更をソース配列に反映させる様な動作を実現したいです。
(DataGridの変更内容が残る形にしたい)
ご協力をお願い致します。
該当のソースコード
XAML
1<DataGrid x:Name="dataGrid" Margin="10" AutoGenerateColumns="False" SourceUpdated="DataGrid_SourceUpdated"> 2 <DataGrid.Columns> 3 <DataGridTextColumn Header="Name" Width="80" 4 Binding="{Binding Data_Name, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" /> 5 <DataGridTextColumn Header="Code" Width="100" 6 Binding="{Binding Data_Code, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" /> 7 <DataGridTextColumn Header="Enable" Width="*" 8 Binding="{Binding Data_Enable, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" /> 9 </DataGrid.Columns> 10</DataGrid> 11<Button Content="Button" HorizontalAlignment="Left" Margin="355,353,0,0" VerticalAlignment="Top" Width="75" Height="56" Click="Button_Click"/>
C#
1public ObservableCollection<DataGrid_Data> Source_DG = new ObservableCollection<DataGrid_Data>(); 2 3public string[] ItemNames = { "AAA", "BBB", "CCC", "DDD" }; 4public string[] ItemCodes = { "aaa", "bbb", "ccc", "ddd" }; 5public string[] ItemEnables = { "111", "222", "333", "444" }; 6 7public MainWindow() 8{ 9 InitializeComponent(); 10 11 for(int i = 0; i < ItemNames.Length; i++) 12 { 13 Source_DG.Add(new DataGrid_Data(ItemNames[i], ItemCodes[i], ItemEnables[i]) { }); 14 } 15 16 dataGrid.ItemsSource = Source_DG; 17} 18 19public class DataGrid_Data 20{ 21 public string Data_Name { get; set; } 22 public string Data_Code { get; set; } 23 public string Data_Enable { get; set; } 24 25 public DataGrid_Data(string Name, string Code, string Enable) 26 { 27 Data_Name = Name; 28 Data_Code = Code; 29 Data_Enable = Enable; 30 } 31 32} 33 34private void DataGrid_SourceUpdated(object sender, DataTransferEventArgs e) 35{ 36} 37 38private void Button_Click(object sender, RoutedEventArgs e) 39{ 40 Source_DG.Clear(); 41 42 for (int i = 0; i < ItemNames.Length; i++) 43 { 44 Source_DG.Add(new DataGrid_Data(ItemNames[i], ItemCodes[i], ItemEnables[i]) { }); 45 } 46 47 dataGrid.ItemsSource = Source_DG; 48}
前提・実現したいこと(追記)
上記のコードから、DataGridのソース元を配列から2次元Listに変更しました。
該当のソースコード
C#
1public ObservableCollection<DataGrid_Data> Source_DG = new ObservableCollection<DataGrid_Data>(); 2 3public List<List<string>> ItemNames = new List<List<string>>(); //リスト化 4public List<List<string>> ItemCodes = new List<List<string>>(); //リスト化 5public List<List<string>> ItemEnables = new List<List<string>>(); //リスト化 6 7 8public string[] str_ItemNames = { "AAA", "BBB", "CCC", "DDD" }; 9public string[] str_ItemCodes = { "aaa", "bbb", "ccc", "ddd" }; 10public string[] str_ItemEnables = { "111", "222", "333", "444" }; 11public string[] str_Dummys = { "0", "00", "000", "0000" }; 12 13public int group = 0; 14 15public MainWindow() 16{ 17 InitializeComponent(); 18 19 20 Init_ItemList(); 21 22 23 for (int i = 0; i < str_ItemCodes.Length; i++) 24 { 25 Source_DG.Add(new DataGrid_Data(ItemNames[group][i], ItemCodes[group][i], ItemEnables[group][i]) { }); 26 } 27 28 dataGrid.ItemsSource = Source_DG; 29} 30 31public void Init_ItemList() 32{ 33 for (int i = 0; i < 4; i++) 34 { 35 ItemNames.Add(new List<string>()); 36 ItemCodes.Add(new List<string>()); 37 ItemEnables.Add(new List<string>()); 38 } 39 40 ItemNames[group].AddRange(str_ItemNames); 41 ItemCodes[group].AddRange(str_ItemCodes); 42 ItemEnables[group].AddRange(str_ItemEnables); 43 44 ItemNames[group + 1].AddRange(str_Dummys); 45 ItemCodes[group + 1].AddRange(str_Dummys); 46 ItemEnables[group + 1].AddRange(str_Dummys); 47 48 ItemNames[group +2].AddRange(str_ItemNames); 49 ItemCodes[group +2].AddRange(str_ItemCodes); 50 ItemEnables[group +2].AddRange(str_ItemEnables); 51} 52 53 54public class DataGrid_Data 55{ 56 public string Data_Name { get; set; } 57 public string Data_Code { get; set; } 58 public string Data_Enable { get; set; } 59 60 public DataGrid_Data(string Name, string Code, string Enable) 61 { 62 Data_Name = Name; 63 Data_Code = Code; 64 Data_Enable = Enable; 65 } 66} 67 68private void Button1_Click(object sender, RoutedEventArgs e) 69{ 70 group = int.Parse(TextBox.Text); 71 72 Source_DG.Clear(); 73 74 for (int i = 0; i < str_ItemCodes.Length; i++) 75 { 76 Source_DG.Add(new DataGrid_Data(ItemNames[group][i], ItemCodes[group][i], ItemEnables[group][i]) { }); 77 } 78 79 dataGrid.ItemsSource = Source_DG; 80} 81 82private void Button2_Click(object sender, RoutedEventArgs e) 83{ 84 /* 85 ItemNames = Source_DG.Select(x => x.Data_Name).ToArray(); 86 ItemCodes = Source_DG.Select(x => x.Data_Code).ToArray(); 87 ItemEnables = Source_DG.Select(x => x.Data_Enable).ToArray(); 88 */ 89}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/24 12:38
2020/04/25 05:26