Q&A
実現したいこと
- WPFでクラスのメンバ変数にバインドする
前提
TextWindow.xaml
xml
1<Window x:Class="TestWindow" 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:Test" 7 mc:Ignorable="d" 8 Title="TestWindow" Height="300" Width="400"> 9 <Window.DataContext> 10 <local:TestViewModel/> 11 </Window.DataContext> 12 <Grid> 13 <Label x:Name="TestLabel" Content="{Binding Data.NameA}"/> 14 </Grid> 15</Window>
TextWindowViewModel.cs
cs
1class TestViewModel : INotifyPropertyChanged 2{ 3 public event PropertyChangedEventHandler PropertyChanged; 4 public TestData _Data= new(); 5 6 // データ 7 public TestData Data 8 { 9 get { return _Data; } 10 set { _Data = value; } 11 } 12}
TestData.cs
cs
1class TestData 2{ 3 public string NameA = "AAA"; 4 public string NameB = "BBB"; 5 public string NameC = "CCC"; 6}
発生している問題
上記コードの通り、TestLabelというラベルに、TestDataのメンバ変数であるNameAをバインドしようとしています。
しかし、NameAの値が変わっても、TestLabelが更新されません。
このように、バインド先をクラスのメンバ変数にする方法はありますでしょうか?
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/03/06 05:14