A problem occurred not long ago. During binding, I planned to set all the data model classes to the internal type for module encapsulation. However, binding does not work after internal is set. The Code is as follows:
Data Model section:
[Csharp]
Public abstract class yybind: INotifyPropertyChanged
{
Public event PropertyChangedEventHandler PropertyChanged;
Public void OnPropertyChanged (string propname)
{
If (this. PropertyChanged! = Null)
{
PropertyChanged (this, new PropertyChangedEventArgs (propname ));
}
}
}
Public class MainModel: policybind
{
Private string ishowTest = string. Empty;
Private string pshowTest = string. Empty;
Internal string IShowTest
{
Get {return this. ishowTest ;}
Set
{
This. ishowTest = value;
This. OnPropertyChanged ("IShowTest ");
}
}
Public string PShowTest
{
Get {return this. pshowTest ;}
Set
{
This. pshowTest = value;
This. OnPropertyChanged ("PShowTest ");
}
}
}
Public abstract class yybind: INotifyPropertyChanged
{
Public event PropertyChangedEventHandler PropertyChanged;
Public void OnPropertyChanged (string propname)
{
If (this. PropertyChanged! = Null)
{
PropertyChanged (this, new PropertyChangedEventArgs (propname ));
}
}
}
Public class MainModel: policybind
{
Private string ishowTest = string. Empty;
Private string pshowTest = string. Empty;
Internal string IShowTest
{
Get {return this. ishowTest ;}
Set
{
This. ishowTest = value;
This. OnPropertyChanged ("IShowTest ");
}
}
Public string PShowTest
{
Get {return this. pshowTest ;}
Set
{
This. pshowTest = value;
This. OnPropertyChanged ("PShowTest ");
}
}
} Page section:
[Html]
<Window x: Class = "TestInternalBinding. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "436" Width = "803">
<Grid>
<TextBox Height = "30" HorizontalAlignment = "Left" Margin = "130,120, 186" Name = "textBox1" VerticalAlignment = "Top" Width ="
Text = "{Binding PShowTest, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}"/>
<TextBox Height = "30" HorizontalAlignment = "Left" Margin = "130,170, 186" Name = "textBox2" VerticalAlignment = "Top" Width ="
Text = "{Binding IShowTest, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}"/>
<Button Content = "public binding" Height = "23" HorizontalAlignment = "Left" Margin = "333,127," Name = "button1"
VerticalAlignment = "Top" Width = "85" Click = "button#click"/>
<Button Content = "internal binding" Height = "23" HorizontalAlignment = "Left" Margin = "333,174," Name = "button2"
VerticalAlignment = "Top" Width = "85" Click = "button2_Click"/>
</Grid>
</Window>
<Window x: Class = "TestInternalBinding. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "436" Width = "803">
<Grid>
<TextBox Height = "30" HorizontalAlignment = "Left" Margin = "130,120, 186" Name = "textBox1" VerticalAlignment = "Top" Width ="
Text = "{Binding PShowTest, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}"/>
<TextBox Height = "30" HorizontalAlignment = "Left" Margin = "130,170, 186" Name = "textBox2" VerticalAlignment = "Top" Width ="
Text = "{Binding IShowTest, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}"/>
<Button Content = "public binding" Height = "23" HorizontalAlignment = "Left" Margin = "333,127," Name = "button1"
VerticalAlignment = "Top" Width = "85" Click = "button#click"/>
<Button Content = "internal binding" Height = "23" HorizontalAlignment = "Left" Margin = "333,174," Name = "button2"
VerticalAlignment = "Top" Width = "85" Click = "button2_Click"/>
</Grid>
</Window>
Background:
[Csharp]
Public partial class MainWindow: Window
{
Private MainModel data = new MainModel ();
Public MainWindow ()
{
InitializeComponent ();
This. DataContext = data;
}
Private void button#click (object sender, RoutedEventArgs e)
{
String showstring = string. IsNullOrEmpty (data. PShowTest )? "Null": data. PShowTest;
MessageBox. Show ("currently public binding, input content:" + showstring );
}
Private void button2_Click (object sender, RoutedEventArgs e)
{
String showstring = string. IsNullOrEmpty (data. IShowTest )? "Null": data. IShowTest;
MessageBox. Show ("currently internal binding, input content:" + showstring );
}
}
Public partial class MainWindow: Window
{
Private MainModel data = new MainModel ();
Public MainWindow ()
{
InitializeComponent ();
This. DataContext = data;
}
Private void button#click (object sender, RoutedEventArgs e)
{
String showstring = string. IsNullOrEmpty (data. PShowTest )? "Null": data. PShowTest;
MessageBox. Show ("currently public binding, input content:" + showstring );
}
Private void button2_Click (object sender, RoutedEventArgs e)
{
String showstring = string. IsNullOrEmpty (data. IShowTest )? "Null": data. IShowTest;
MessageBox. Show ("currently internal binding, input content:" + showstring );
}
} When the text is input to the text box, trigger the button to click the time, for example:
1. If the field is public,
2. If the field is internal,
It can be seen that after the field is internal, binding does not work. Therefore, when binding, the data model class must be public.