2022/01/30 10:40
質問の末尾に解決コードを追記しました。
課題
自作クラスのインスタンスをLabel.ContentやTextBlock.Textにバインディングしています。
初期表示では期待通りの文字列が表示されますが、バインディングソース側を更新してPropertyChangedイベントを発行しても、画面に表示される文字列が更新されません。
どうしたら更新されるようになるでしょうか。
試したこと
以下は効果がありませんでした:
- 番号リスト 自作クラスからstringへ変換するIValueConverterを作成して設定してみた
- DataTemplateを設定してみた
- 自作クラス自体にINotifyPropertyChangedを実装してみた
上記1については以下を参考にしてみました:
https://stackoverflow.com/questions/6020246/wpf-binding-to-custom-class-property-propertychanged-is-fired-but-view-does-no
実際のコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Globalization; 5using System.Runtime.CompilerServices; 6using System.Windows.Data; 7 8namespace I18nTest 9{ 10 public abstract class Notifiable : INotifyPropertyChanged 11 { 12 public event PropertyChangedEventHandler? PropertyChanged; 13 14 protected void RaisePropertyChanged([CallerMemberName] string? propertyName = null) 15 => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 16 17 protected bool SetProperty<T>(ref T storage, T value, 18 [CallerMemberName] string? propertyName = null) 19 { 20 if(EqualityComparer<T>.Default.Equals(storage, value)) 21 { 22 return false; 23 } 24 else 25 { 26 storage = value; 27 RaisePropertyChanged(propertyName); 28 return true; 29 } 30 } 31 } 32 33 public class I18nStr : Notifiable 34 { 35 private readonly string[] _values; 36 private int _language; 37 public int Language 38 { 39 set 40 { 41 if(_language != value) 42 { 43 _language = value; 44 RaisePropertyChanged(nameof(Value)); 45 } 46 } 47 get => _language; 48 } 49 public I18nStr(params string[] values) => _values = values; 50 public string Value => _values[Language]; 51 public override string ToString() => _values[Language]; 52 public static implicit operator string(I18nStr str) => str.ToString(); 53 } 54 55 public class I18nStrConverter : IValueConverter 56 { 57 public object Convert(object value, Type targetType, object parameter, CultureInfo _) 58 => value.ToString()!; 59 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo _) 60 => throw new NotImplementedException(); 61 } 62 63 public class ViewModel : Notifiable 64 { 65 public I18nStr MyText => new I18nStr("en", "jp"); 66 public IEnumerable<string> Languages => new string[] { "英語", "日本語" }; 67 private int _selectedLanguage; 68 public int SelectedLanguage 69 { 70 set 71 { 72 if(_selectedLanguage != value) 73 { 74 _selectedLanguage = value; 75 RaisePropertyChanged(); 76 77 MyText.Language = _selectedLanguage; 78 RaisePropertyChanged(nameof(MyText)); 79 } 80 } 81 get => _selectedLanguage; 82 } 83 } 84}
xml
1<Window x:Class="I18nTest.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:I18nTest" 7 mc:Ignorable="d" 8 Title="MainWindow" 9 MinWidth="300" 10 SizeToContent="WidthAndHeight" 11 > 12 <Window.Resources> 13 <local:I18nStrConverter x:Key="I18nStrConverter" /> 14 </Window.Resources> 15 <Window.DataContext> 16 <local:ViewModel /> 17 </Window.DataContext> 18 <StackPanel> 19 <ComboBox 20 ItemsSource="{Binding Languages}" 21 SelectedIndex="{Binding SelectedLanguage, Mode=TwoWay}" 22 /> 23 <Label 24 Content="{Binding MyText, 25 Converter={StaticResource I18nStrConverter}, 26 UpdateSourceTrigger=PropertyChanged}" 27 /> 28 <Label 29 Content="{Binding MyText.Value}" 30 /> 31 <ContentControl 32 Content="{Binding MyText}" 33 > 34 <ContentControl.ContentTemplate> 35 <DataTemplate DataType="{x:Type local:I18nStr}"> 36 <TextBlock Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" /> 37 </DataTemplate> 38 </ContentControl.ContentTemplate> 39 </ContentControl> 40 </StackPanel> 41</Window>
環境
Visual Studio Professional 2019 ver. 16.11.9
.NET 5
解決時のコード
xml
1<ComboBox 2 ItemsSource="{Binding Languages}" 3 SelectedIndex="{Binding SelectedLanguage, Mode=TwoWay}" 4 /> 5<Label 6 Content="{Binding MyText.Value}" 7 />
本番ではModelからイベント通知を受けて更新を行いたいので、Languageをオブジェクトにしてみました。本番ではRx.NETを使って、ViewModelの破棄と同時にイベント購読キーも破棄します。
C#
1public class Language 2{ 3 public delegate void ChangedHandler(int language); 4 public event ChangedHandler? Changed; 5 private int _value; 6 public int Value 7 { 8 set 9 { 10 if(_value != value) 11 { 12 _value = value; 13 Changed?.Invoke(_value); 14 } 15 } 16 get => _value; 17 } 18} 19 20public abstract class Notifiable : INotifyPropertyChanged 21{ 22 // 前述のコードと同じなので、略。 23} 24 25public class I18nStr : Notifiable 26{ 27 private int _language; 28 private string[] _values; 29 public I18nStr(Language language, params string[] values) 30 { 31 _language = language.Value; 32 _values = values; 33 language.Changed += ChangeLanguage; 34 } 35 public string Value => _values[_language]; 36 private void ChangeLanguage(int language) 37 { 38 if(_language != language) 39 { 40 _language = language; 41 RaisePropertyChanged(nameof(Value)); 42 } 43 } 44} 45 46public class ViewModel : Notifiable 47{ 48 private readonly Language _language = new Language(); 49 public I18nStr MyText { get; } 50 public IEnumerable<string> Languages => new string[] { "英語", "日本語" }; 51 public int SelectedLanguage 52 { 53 set 54 { 55 if(_language.Value != value) 56 { 57 _language.Value = value; 58 RaisePropertyChanged(); 59 } 60 } 61 get => _language.Value; 62 } 63 64 public ViewModel() 65 { 66 MyText = new I18nStr(_language, "en", "jp"); 67 } 68}

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/30 00:43
2022/01/30 00:58
2022/01/30 01:12
2022/01/30 01:24
2022/01/30 01:43