前提・実現したいこと
C#のWPFで計算した数値を表示したいだけなんですが,表示したい数字は3ケタもあるので配列などを使いたいです.
ボタンと別途textboxを配置してボタンクリックで一部のtextboxから数値を取りだして,計算し,他のtextboxへ配列で一気に表示したいです
発生している問題・エラーメッセージ
teratail上に似たものを発見してそれを試している最中で,クリックで対応するための書き方がわからなかったです.
GridContext.Text = text[ii];の部分が動作するにはどうすれば良いんでしょうか.
エラーメッセージ
該当のソースコード
XAML
1<Window x:Class="Musen.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:Musen" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="700" Width="800"> 9 <Grid Margin="20" x:Name="maingrid"> 10 <Grid.ColumnDefinitions> 11 <ColumnDefinition Width="*"/> 12 <ColumnDefinition Width="*"/> 13 <ColumnDefinition Width="*"/> 14 <ColumnDefinition Width="*"/> 15 <ColumnDefinition Width="*"/> 16 <ColumnDefinition Width="*"/> 17 <ColumnDefinition Width="*"/> 18 <ColumnDefinition Width="*"/> 19 <ColumnDefinition Width="*"/> 20 <ColumnDefinition Width="*"/> 21 <ColumnDefinition Width="*"/> 22 <ColumnDefinition Width="*"/> 23 </Grid.ColumnDefinitions> 24 <Grid.RowDefinitions> 25 <RowDefinition Height="*"/> 26 <RowDefinition Height="2*"/> 27 <RowDefinition Height="*"/> 28 <RowDefinition Height="*"/> 29 <RowDefinition Height="*"/> 30 <RowDefinition Height="*"/> 31 <RowDefinition Height="*"/> 32 <RowDefinition Height="*"/> 33 <RowDefinition Height="*"/> 34 <RowDefinition Height="*"/> 35 <RowDefinition Height="*"/> 36 <RowDefinition Height="*"/> 37 <RowDefinition Height="*"/> 38 <RowDefinition Height="*"/> 39 </Grid.RowDefinitions> 40 <TextBox Text="{Binding Texts[0], Mode=OneWay}" Grid.Column="0" Grid.Row="2" Name="TextBox1" Height="30" Width="50" /> 41 <TextBox Text="{Binding Texts[1], Mode=OneWay}" Grid.Column="0" Grid.Row="3" Name="TextBox2" Height="30" Width="50"/> 42 <TextBox Text="{Binding Texts[2], Mode=OneWay}" Grid.Column="0" Grid.Row="4" Name="TextBox3" Height="30" Width="50"/> 43 <TextBox Text="{Binding Texts[3], Mode=OneWay}" Grid.Column="0" Grid.Row="5" Name="TextBox4" Height="30" Width="50"/> 44・ 45・ 46・ 47計150近く格子状に配置.(上から2行は入力用のtextboxのために空けてます.) 48・ 49 50 <TextBox Height="40" Width="50" Name="TextBox0" Text="0" 51 HorizontalAlignment="Left" Grid.Column="1" Grid.Row="0" /> 52 <TextBox Name="Hirogari" Width="50" HorizontalAlignment="Left" 53 Text="0" Grid.Column="0" Grid.Row="0" /> 54 <Button Content="Click" Height="23" Width="70" Name="Button1" 55 HorizontalAlignment="Left" Grid.Row="0" Grid.Column="4" Click="Button_Click_1" /> 56 </Grid> 57</Window> 58
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Windows; 5using System.Windows.Controls; 6 7namespace Musen 8{ 9 /// <summary> 10 /// MainWindow.xaml の相互作用ロジック 11 /// </summary> 12 13 public class GridContext 14 { 15 public string[] Texts { get; private set; } 16 17 public GridContext() 18 { 19 20 } 21 } 22 23 24 public partial class MainWindow : Window 25 { 26 static int hComm; 27 static string wDATA; 28 static Int32 dLen, wLen; 29 static Boolean bRet; 30 static double deltaL; 31 static double deltaT; 32 static int deltaS; 33 static double dx; 34 static double dy; 35 static double dr; 36 static double dl; 37 static double dl0; 38 static int ii; 39 static int[] deltaSS = new int[144]; 40 static int[] deltaSSS = new int[144]; 41 static int deltaSS_min; 42 static int Pulsefreq0; 43 static int gain0; 44 static double lx; 45 static double ly; 46 47 static string[] text = new string[145]; 48 49 public MainWindow() 50 { 51 InitializeComponent(); 52 this.maingrid.DataContext = new GridContext(); 53 } 54 55 private void Button_Click_1(object sender, RoutedEventArgs e) 56 { 57 58 // 開始 59 60 for (int i4 = 1; i4 <= 2; i4++) 61 { 62 for (int i3 = 1; i3 <= 2; i3++) 63 { 64 for (int i1 = 1; i1 <= 6; i1++) 65 { 66 for (int i2 = 1; i2 <= 6; i2++) 67 { 68 //計算した結果をdeltaSに入れる部分 69 70 text[ii] = deltaS.ToString(); 71 //こんな以下のTextに入れられずに表示できないので解決策がほしいです. 72 GridContext.Text = text[ii]; 73 74 75 } 76 } 77 } 78 } 79 //deltaSS_min =0; 80 //終了 81 82 } 83 } 84} 85
試したこと
teratail上にあったものの二つ目のbindを回答欄に見つけたのでそれをコピペしましたが,クリックで毎回動作する今回のパターンに対応させる方法が分からなかった.
C#のコード中のGridContext.Text = text[ii];でそれぞれのTextに表示させたい.
補足情報(FW/ツールのバージョンなど)
Free版のVisalStudio2015を使用中