こんにちは。
Windows10でWPFのアプリケーションを開発しています。
Visual Studio 2015 Communityを使っています。
###前提・実現したいこと
JSONのデータをtextboxと相互にBindしたいです。
###試したこと
C#
1 Binding bind1 = new Binding(); 2 bind1.Path = new PropertyPath(textBox1.Text); 3 bind1.Source = template.template_no.ToString(); 4 5 Binding bind2 = new Binding(); 6 bind2.Path = new PropertyPath(template.template_name); 7 bind2.Source = textBox2.Text;
###発生している問題・エラーメッセージ
どちらもTextBoxの内容はTextBoxのまま。
単純に、
textBox1.Text = template.template_no.ToString();
textBox2.Text = template.template_name;
なら値を入れることができる。
###該当のソースコード
xaml
1<Window x:Class="JSON.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:JSON" 7 mc:Ignorable="d" 8 Loaded="Window_Loaded" 9 Title="MainWindow" Height="350" Width="525"> 10 <Grid> 11 <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="173,56,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="247"/> 12 <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="173,84,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="247"/> 13 <TextBox x:Name="textBox3" HorizontalAlignment="Left" Height="23" Margin="173,112,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="247"/> 14 <TextBox x:Name="textBox4" HorizontalAlignment="Left" Height="23" Margin="173,140,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="247"/> 15 <TextBox x:Name="textBox5" HorizontalAlignment="Left" Height="23" Margin="173,168,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="247"/> 16 <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="350,233,0,0" VerticalAlignment="Top" Width="75"/> 17 18 </Grid> 19</Window>
JSON
1{ 2 "template": [ 3 { 4 "template_no": 3, 5 "template_name": "説明書", 6 "description": "説明書説明用資料01", 7 "img": "other", 8 "disp_start_date": "2017/09/05 10:30" 9 } 10 ] 11}
C#
1using System.IO; 2using System.Text; 3using System.Windows; 4using Newtonsoft.Json; 5using System.Windows.Data; 6 7namespace JSON { 8 public partial class MainWindow : Window { 9 public MainWindow() { 10 InitializeComponent(); 11 } 12 13 private void Window_Loaded(object sender, RoutedEventArgs e) { 14 string file = @"C:\json.json"; 15 string jsonstring = File.ReadAllText(file, Encoding.UTF8); 16 Templates templates = JsonConvert.DeserializeObject<Templates>(jsonstring); 17 foreach(Template template in templates.template) { 18 Binding bind1 = new Binding(); 19 bind1.Path = new PropertyPath(textBox1.Text); 20 bind1.Source = template.template_no.ToString(); 21 22 Binding bind2 = new Binding(); 23 bind2.Path = new PropertyPath(template.template_name); 24 bind2.Source = textBox2.Text; 25 26 //textBox1.Text = template.template_no.ToString(); 27 //textBox2.Text = template.template_name; 28 textBox3.Text = template.description; 29 textBox4.Text = template.img; 30 textBox5.Text = template.disp_start_date; 31 } 32 } 33 } 34} 35 36 37 38public class Templates { 39 public Template[] template { get; set; } 40} 41 42public class Template { 43 public int template_no { get; set; } 44 public string template_name { get; set; } 45 public string description { get; set; } 46 public string img { get; set; } 47 public string disp_start_date { get; set; } 48} 49
###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio Community 2015
Version 14.0.25424.00 Update 3
Microsoft .NET Framework
Version 4.6.01038
インストールしているバージョン:Community
Visual C# 2015 00322-20000-00000-AA575
Microsoft Visual C# 2015
です。
よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/31 06:44
2017/01/31 13:39