1点目
xamlの間違いを直します。
xml
1 < TextBlock x: Name = " TextBlock_Result " Text = " {Binding Path=printText.Result} " />
2 < TextBlock x: Name = " TextBlock_Filename " Text = " {Binding filename.Filename} " />
↓
xml
1 < TextBlock x: Name = " TextBlock_Result " Text = " {Binding PrintText.Result} " />
2 < TextBlock x: Name = " TextBlock_Filename " Text = " {Binding PrintFilename.Filename} " />
バインディングのエラーは出力ウィンドウに出るだけで、特に止まったりしないので気が付きにくいです。
System.Windows.Data Error: 40 : BindingExpression path error: 'printText' property not found on 'object' ''MainWindowViewModel' (HashCode=64554036)'. BindingExpression:Path=printText.Result; DataItem='MainWindowViewModel' (HashCode=64554036); target element is 'TextBlock' (Name='TextBlock_Result'); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'filename' property not found on 'object' ''MainWindowViewModel' (HashCode=64554036)'. BindingExpression:Path=filename.Filename; DataItem='MainWindowViewModel' (HashCode=64554036); target element is 'TextBlock' (Name='TextBlock_Filename'); target property is 'Text' (type 'String')
のようなエラーが出ているはずです。
2点目
バインディングを使って表示したいのでしょうから、変更通知(INotifyPropertyChanged
)の実装が必要になります(ViewModelBase・BindableBase・Observable等名前はいろいろだったりしますが、中身は大体同じで定型コードです)
MainWindowViewModel
のPrintText
・PrintFilename
を丸ごと入れ替えるような想定であればこんな感じになります。
cs
1 using System . ComponentModel ;
2 using System . Runtime . CompilerServices ;
3 using System . Windows ;
4 using Microsoft . Win32 ;
5
6 namespace Questions233114
7 {
8 class PrintFilename
9 {
10 public string Filename { get ; set ; }
11 }
12
13 class PrintText
14 {
15 public string Result { get ; set ; }
16 }
17
18 class MainWindowViewModel : Observable
19 {
20 public PrintText PrintText { get => _PrintText ; set => Set ( ref _PrintText , value ) ; }
21 private PrintText _PrintText ;
22 public PrintFilename PrintFilename { get => _PrintFilename ; set => Set ( ref _PrintFilename , value ) ; }
23 private PrintFilename _PrintFilename ;
24
25 public MainWindowViewModel ( )
26 {
27 PrintText = new PrintText ( ) ;
28 PrintFilename = new PrintFilename ( ) ;
29 }
30 }
31
32 abstract class Observable : INotifyPropertyChanged
33 {
34 public event PropertyChangedEventHandler PropertyChanged ;
35 protected bool Set < T > ( ref T storage , T value , [ CallerMemberName ] string propertyName = null )
36 {
37 if ( Equals ( storage , value ) ) return false ;
38
39 storage = value ;
40 OnPropertyChanged ( propertyName ) ;
41 return true ;
42 }
43 protected void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null )
44 => PropertyChanged ?. Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
45 }
46
47 public partial class MainWindow : Window
48 {
49 private readonly MainWindowViewModel vm ;
50 public MainWindow ( )
51 {
52 InitializeComponent ( ) ;
53
54 vm = new MainWindowViewModel ( ) ;
55 DataContext = vm ;
56
57 var printText = new PrintText { Result = "OK" } ;
58 vm . PrintText = printText ;
59
60 MessageBox . Show ( vm . PrintText . Result ) ;
61 }
62
63 private void Data_Button_Click ( object sender , RoutedEventArgs e )
64 {
65 var oFileDialog = new OpenFileDialog { Filter = "Excel (*.xlsx)|*.xlsx" } ;
66
67 if ( oFileDialog . ShowDialog ( ) == false )
68 {
69 return ;
70 }
71
72 var filename = new PrintFilename { Filename = oFileDialog . FileName } ;
73 vm . PrintFilename = filename ;
74
75 MessageBox . Show ( vm . PrintFilename . Filename ) ;
76 }
77 }
78 }
Filename・Resultを直接変えるのであればこんな感じになります。
cs
1 using System . ComponentModel ;
2 using System . Runtime . CompilerServices ;
3 using System . Windows ;
4 using Microsoft . Win32 ;
5
6 namespace Questions233114
7 {
8 class PrintFilename : Observable
9 {
10 public string Filename { get => _Filename ; set => Set ( ref _Filename , value ) ; }
11 private string _Filename ;
12 }
13
14 class PrintText : Observable
15 {
16 public string Result { get => _Result ; set => Set ( ref _Result , value ) ; }
17 private string _Result ;
18 }
19
20 class MainWindowViewModel
21 {
22 public PrintText PrintText { get ; private set ; }
23 public PrintFilename PrintFilename { get ; private set ; }
24
25 public MainWindowViewModel ( )
26 {
27 PrintText = new PrintText ( ) ;
28 PrintFilename = new PrintFilename ( ) ;
29 }
30 }
31
32 abstract class Observable : INotifyPropertyChanged
33 {
34 public event PropertyChangedEventHandler PropertyChanged ;
35 protected bool Set < T > ( ref T storage , T value , [ CallerMemberName ] string propertyName = null )
36 {
37 if ( Equals ( storage , value ) ) return false ;
38
39 storage = value ;
40 OnPropertyChanged ( propertyName ) ;
41 return true ;
42 }
43 protected void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null )
44 => PropertyChanged ?. Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
45 }
46
47 public partial class MainWindow : Window
48 {
49 private readonly MainWindowViewModel vm ;
50
51 public MainWindow ( )
52 {
53 InitializeComponent ( ) ;
54 vm = new MainWindowViewModel ( ) ;
55 DataContext = vm ;
56
57 vm . PrintText . Result = "OK" ;
58
59 MessageBox . Show ( vm . PrintText . Result ) ;
60 }
61
62 private void Data_Button_Click ( object sender , RoutedEventArgs e )
63 {
64 var oFileDialog = new OpenFileDialog { Filter = "Excel (*.xlsx)|*.xlsx" } ;
65
66 if ( oFileDialog . ShowDialog ( ) == false )
67 {
68 return ;
69 }
70
71 vm . PrintFilename . Filename = oFileDialog . FileName ;
72
73 MessageBox . Show ( vm . PrintFilename . Filename ) ;
74 }
75 }
76 }
どちらのパターンもあり得ますが、どちらがいいかはこれだけだとちょっと判断できません。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/04 00:01
2020/01/04 05:22
2020/01/05 13:44