前提・実現したいこと
Visual Studio 2019を使ってWPFのBindingを試しています。
入れ子になったBindingを試したいのですが、うまく動作しません。
発生している問題・エラーメッセージ
デバッグでコードを追うと、PropertyChangedは走っていてsetで値を設定しているのですが、値をTextBoxに反映できません。
アドレスお願いします。
該当のソースコード
XAML
1<Window x:Class="NestedBinding.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:NestedBinding" 7 xmlns:vm="clr-namespace:Order" 8 mc:Ignorable="d" 9 Title="MainWindow" Height="450" Width="800" 10 Loaded="Window_Loaded"> 11 <!--試行錯誤 あってもなくても動作は変化なし--> 12 <Window.DataContext> 13 <vm:OrderViewModel /> 14 </Window.DataContext> 15 <StackPanel > 16 <TextBox Width="300" Text="{Binding Path=OrderDetail/GoodsName, Mode=TwoWay}" /> 17 <TextBox Width="300" Text="{Binding Path=OrderDetail.GoodsName, Mode=TwoWay}" /> 18 <TextBox Width="300" Text="{Binding Path=OrderDetail, Mode=TwoWay}" /> 19 <TextBox Width="300" Text="{Binding Path=GoodsName, Mode=TwoWay}" /> 20 <TextBox x:Name="text1" Width="300" /> 21 <TextBox x:Name="text2" Width="300" /> 22 <TextBox x:Name="text3" Width="300" /> 23 <TextBox Width="300" Text="{Binding Path=ParentGoodsName, Mode=TwoWay}" /> 24 <Button Height="50" Click="Button_Click"/> 25 </StackPanel> 26</Window> 27
C#
1using Order; 2using System.Windows; 3using System.Windows.Controls; 4 5namespace NestedBinding 6{ 7 public partial class MainWindow : Window 8 { 9 OrderViewModel ViewModel = new OrderViewModel(); 10 public MainWindow() 11 { 12 InitializeComponent(); 13 this.DataContext = ViewModel; 14 15 text1.SetBinding(TextBox.TextProperty, "ViewModel.OrderDetail.GoodsName");//NG 16 text2.SetBinding(TextBox.TextProperty, "OrderViewModel.OrderDetail.GoodsName");//NG 17 text3.SetBinding(TextBox.TextProperty, "OrderDetail.GoodsName");//NG 18 } 19 20 private void Window_Loaded(object sender, RoutedEventArgs e) 21 { 22 ViewModel.OrderDetail.GoodsName = "Loaded";//NG 23 ViewModel.ParentGoodsName = "ParentLoaded";//NG 24 } 25 26 private void Button_Click(object sender, RoutedEventArgs e) 27 { 28 ViewModel.OrderDetail.GoodsName = "ButtonClick";//NG 29 ViewModel.ParentGoodsName = "ParentClick";//NG 30 text1.Text = "direct"+ ViewModel.OrderDetail.GoodsName;//OK 31 text2.Text = "direct"+ViewModel.ParentGoodsName;//OK 32 } 33 } 34} 35
C#
1using System.ComponentModel; 2 3namespace Order 4{ 5 public class OrderViewModel : INotifyPropertyChanged 6 { 7 public OrderDetailModel OrderDetail = new OrderDetailModel(); 8 9 private string parentgoodsname; 10 public string ParentGoodsName 11 { 12 get 13 { 14 return parentgoodsname; 15 } 16 set 17 { 18 parentgoodsname = value; 19 OnPropertyChanged(ParentGoodsName); 20 } 21 } 22 23 24 public event PropertyChangedEventHandler PropertyChanged; 25 26 protected void OnPropertyChanged(string name) 27 { 28 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 29 } 30 } 31} 32
C#
1using System.ComponentModel; 2 3namespace Order 4{ 5 public class OrderDetailModel : INotifyPropertyChanged 6 { 7 private string goodsname; 8 9 public string GoodsName 10 { 11 get => goodsname; 12 set 13 { 14 goodsname = value; 15 OnPropertyChanged(GoodsName); 16 } 17 } 18 19 public event PropertyChangedEventHandler PropertyChanged; 20 21 protected void OnPropertyChanged(string name) 22 { 23 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 24 } 25 } 26}
試したこと
C#とxamlでいろいろ作ってみました。
単純にtext1.Text=値なら表示できました。
補足情報(FW/ツールのバージョンなど)
Microsoft Visual Studio Community 2019
Version 16.1.3
VisualStudio.16.Release/16.1.3+29009.5
Microsoft .NET Framework
Version 4.7.03190
インストールされているバージョン:Community
Target Framework .NET Framework 4.7.2