WPF-14: The data model in the binding must be public

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.