Q&A
実現したいこと
- データグリッドからラジオボタンの値を取得したい。
前提
データグリッドの値をラジオボタンも含め、全て取得したい。
発生している問題・エラーメッセージ
System.NullReferenceException: 'オブジェクト参照がオブジェクト インスタンスに設定されていません。' radioButton が null でした。
該当のソースコード
XAML
1<Window x:Class="WpfRadio.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfRadio" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800"> 9 <Grid> 10 <StackPanel Orientation="Vertical"> 11 <StackPanel Orientation="Horizontal"> 12 <Button Width="100" x:Name="Test" Content="Test" Margin="10" Click="Test_Click"/> 13 <Label x:Name="LabelTest" Content="test"/> 14 </StackPanel> 15 16 <DataGrid ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="5" 17 Name="DG" IsReadOnly="false" AutoGenerateColumns="False" FontSize="16" 18 CanUserSortColumns="False" 19 CanUserAddRows="False" 20 CanUserDeleteRows="False" 21 CanUserResizeRows="False" 22 SelectionUnit="Cell" 23 > 24 <DataGrid.Columns> 25 <DataGridTextColumn x:Name="No" Header="No." Binding="{Binding No}"/> 26 <DataGridTextColumn x:Name="Name" Header="名前" Binding="{Binding Name}"/> 27 <!-- DataGridCheckBoxColumn x:Name="Select" Header="選択" Binding="{Binding Select}" /--> 28 <DataGridTemplateColumn Header="選択" x:Name="Temp"> 29 <DataGridTemplateColumn.CellTemplate> 30 <DataTemplate> 31 <RadioButton x:Name="Select" GroupName="sel" IsChecked="False"/> 32 </DataTemplate> 33 </DataGridTemplateColumn.CellTemplate> 34 </DataGridTemplateColumn> 35 </DataGrid.Columns> 36 </DataGrid> 37 </StackPanel> 38 39 </Grid> 40</Window> 41
C#
1using System; 2using System.Collections.ObjectModel; 3using System.Runtime.InteropServices; 4using System.Threading.Tasks; 5using System.Windows; 6using System.Windows.Controls; 7 8namespace WpfRadio 9{ 10 // データグリッド用クラス 11 public class ClsDG 12 { 13 public string No { get; set; } = ""; 14 public string Name { get; set; } = ""; 15 public string Select { get; set; } = ""; 16 17 public ClsDG(int no, string name, bool select) 18 { 19 No = no.ToString(); 20 Name = name; 21 Select = select.ToString(); 22 } 23 } 24 25 //[StructLayout(LayoutKind.Sequential, Pack = 1)] 26 struct StructBase 27 { 28 public int No; 29 public string Name; 30 public bool Select; 31 } 32 33 public partial class MainWindow : Window 34 { 35 ObservableCollection<ClsDG> PDG = new ObservableCollection<ClsDG>(); // グリッド情報 36 StructBase[] st = new StructBase[5]; // グリッドを格納する構造体の配列 37 38 public MainWindow() 39 { 40 InitializeComponent(); 41 DataContext = this; 42 43 DG_5(); 44 } 45 46 // データグリッドの行を表示する 47 private async void DG_5() 48 { 49 for(int i = 0; i < 5; i++) 50 { 51 PDG.Add(new ClsDG(i+1, $"名前{i + 1}", false)); 52 } 53 await Task.Run(() => TaskViewGrid()); 54 } 55 56 private void TaskViewGrid() 57 { 58 Dispatcher.Invoke((Action)(() => 59 { 60 DG.ItemsSource = PDG; 61 })); 62 } 63 64 private void Test_Click(object sender, RoutedEventArgs e) 65 { 66 for (int i = 0; i < 5; i++) 67 { 68 var row = DG.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow; // 1列取得 69 70 // データグリッドの列数を取得 71 int columunCount = DG.Columns.Count; 72 73 string[] strCol = new string[columunCount]; 74 // データグリッドの中身を取得 75 for (int j = 0; j < columunCount; j++) 76 { 77 // セルを取得 78 var cell = DG.Columns[j].GetCellContent(row); 79 80 //var textBlock = cell as TextBlock; 81 //strCol[j] = textBlock.Text; // RadioButtonの値が常にnullになってしまう 82 83 if(j != 2) 84 { 85 var textBlock = cell as TextBlock; 86 strCol[j] = textBlock.Text; 87 } 88 else 89 { // ラジオボタンにしてみても、nullになってしまう 90 var radioButton = cell as RadioButton; 91 strCol[j] = radioButton.IsChecked.ToString(); 92 } 93 } 94 // 格納 95 st[i].No = int.Parse(strCol[0]); 96 st[i].Name = strCol[1]; 97 st[i].Select = bool.Parse(strCol[2]); 98 } 99 } 100 } 101}
試したこと
受け取る型をTextBlock
とRadioButton
で試してみましたが、そもそもセルの中身がnullなのでダメでした。
補足情報(FW/ツールのバージョンなど)
.NET Framework 4.7.2
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/03/08 07:45
2023/03/08 08:46
2023/03/16 00:46
2023/03/16 03:45