WPFのBindingで変数等のBindingについてはINotifyPropertyChangedを実装することで問題無くBindingすることが出来ていますが、
構造体のBinding
クラスのBindingで質問があります。
構造体のObservableCollectionを宣言すると問題無くBindingすることができますが
クラスのObservableCollectionでは変更通知されません(TextBoxにBindingした場合、TextBoxの内容を変更すると変更通知されますが、コードで値を直接変更してもTextBoxの内容が変更されない)
これはなぜでしょうか?
クラス側に何か実装する必要があるのでしょうか?
(クラス側にINotifyPropertyChangedを実装しても結果は同じでBinding先の値が変化しませんでした)
原因がわからず困っています。
よろしくお願い致します。
該当ソース
c#
1 public class clsView 2 { 3 public class TestClass 4 { 5 public int intTest { get; set; } 6 } 7 8 public struct TestStruct 9 { 10 public int intTest { get; set; } 11 } 12 13 14 private ObservableCollection<TestClass> test_Class; 15 private ObservableCollection<TestStruct> test_Struct; 16 17 18 /// <summary> 19 /// コンストラクタ 20 /// </summary> 21 public clsView() 22 { 23 test_Class = new ObservableCollection<TestClass>(); 24 test_Struct = new ObservableCollection<TestStruct>(); 25 26 TestClass cls = new TestClass(); 27 cls.intTest = 100; 28 test_Class.Add(cls); 29 30 TestStruct st = new TestStruct(); 31 st.intTest = 200; 32 test_Struct.Add(st); 33 } 34 35 36 // Class Binding 37 public ObservableCollection<TestClass> View_Class // Bindingされるがコードから変更するとBinding先のTextBoxの内容が変更されない 38 { 39 get { return test_Class; } 40 set { test_Class = value; } 41 } 42 43 // Struct Binding 44 public ObservableCollection<TestStruct> View_Struct // 問題無し 45 { 46 get { return test_Struct; } 47 set { test_Struct = value; } 48 } 49 } 50
c#
1namespace ClassBinding 2{ 3 /// <summary> 4 /// MainWindow.xaml の相互作用ロジック 5 /// </summary> 6 public partial class MainWindow : Window 7 { 8 clsView view; 9 10 public MainWindow() 11 { 12 InitializeComponent(); 13 14 view = new clsView(); 15 16 this.DataContext = view; 17 } 18 19 /// <summary> 20 /// クリックイベントで値を変更 21 /// </summary> 22 /// <param name="sender"></param> 23 /// <param name="e"></param> 24 private void Button_Click(object sender, RoutedEventArgs e) 25 { 26 view.View_Class[0].intTest = 111; // Binding先へ通知されない 27 28 clsView.TestStruct st = view.View_Struct[0]; 29 st.intTest = 222; 30 view.View_Struct[0] = st; // 問題無し 31 } 32 } 33} 34
Xaml
1<Window x:Class="ClassBinding.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:ClassBinding" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="450" Width="800"> 9 <StackPanel 10 VerticalAlignment="Center"> 11 <StackPanel 12 Orientation="Horizontal" 13 HorizontalAlignment="Center"> 14 <TextBlock 15 Text="Class Binding" 16 Width="100" 17 TextAlignment="Right"/> 18 <TextBox 19 Text="{Binding View_Class[0].intTest, Mode=TwoWay}" 20 Width="100" 21 Height="20"/> 22 </StackPanel> 23 <StackPanel 24 Orientation="Horizontal" 25 HorizontalAlignment="Center"> 26 <TextBlock 27 Text="Struct Binding" 28 Width="100" 29 TextAlignment="Right"/> 30 <TextBox 31 Text="{Binding View_Struct[0].intTest, Mode=TwoWay}" 32 Width="100" 33 Height="20"/> 34 </StackPanel> 35 <Button 36 Width="100" 37 Height="20" 38 Click="Button_Click"/> 39 </StackPanel> 40</Window>
回答1件
あなたの回答
tips
プレビュー