不久前遇到一個問題,在綁定的時候打算將數據模型類全部設置為internal類型,進行模塊的封裝。不過當設置為internal之後綁定居然不起作用了。代碼如下:
數據模型部分:
[csharp]
public abstract class NotifyBind : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propname)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propname));
}
}
}
public class MainModel:NotifyBind
{
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 NotifyBind : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propname)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propname));
}
}
}
public class MainModel:NotifyBind
{
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");
}
}
}頁面部分:
[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,0,0" Name="textBox1" VerticalAlignment="Top" Width="186"
Text="{Binding PShowTest,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Height="30" HorizontalAlignment="Left" Margin="130,170,0,0" Name="textBox2" VerticalAlignment="Top" Width="186"
Text="{Binding IShowTest,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="public綁定" Height="23" HorizontalAlignment="Left" Margin="333,127,0,0" Name="button1"
VerticalAlignment="Top" Width="85" Click="button1_Click" />
<Button Content="internal綁定" Height="23" HorizontalAlignment="Left" Margin="333,174,0,0" 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,0,0" Name="textBox1" VerticalAlignment="Top" Width="186"
Text="{Binding PShowTest,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<TextBox Height="30" HorizontalAlignment="Left" Margin="130,170,0,0" Name="textBox2" VerticalAlignment="Top" Width="186"
Text="{Binding IShowTest,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<Button Content="public綁定" Height="23" HorizontalAlignment="Left" Margin="333,127,0,0" Name="button1"
VerticalAlignment="Top" Width="85" Click="button1_Click" />
<Button Content="internal綁定" Height="23" HorizontalAlignment="Left" Margin="333,174,0,0" Name="button2"
VerticalAlignment="Top" Width="85" Click="button2_Click" />
</Grid>
</Window>
後台部分:
[csharp]
public partial class MainWindow : Window
{
private MainModel data = new MainModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = data;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string showstring = string.IsNullOrEmpty(data.PShowTest) ? "空" : data.PShowTest;
MessageBox.Show("當前為public綁定,輸入內容為:" + showstring);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string showstring = string.IsNullOrEmpty(data.IShowTest) ? "空" : data.IShowTest;
MessageBox.Show("當前為internal綁定,輸入內容為:" + showstring);
}
}
public partial class MainWindow : Window
{
private MainModel data = new MainModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = data;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
string showstring = string.IsNullOrEmpty(data.PShowTest) ? "空" : data.PShowTest;
MessageBox.Show("當前為public綁定,輸入內容為:" + showstring);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string showstring = string.IsNullOrEmpty(data.IShowTest) ? "空" : data.IShowTest;
MessageBox.Show("當前為internal綁定,輸入內容為:" + showstring);
}
}當輸入文字到文本框後,分別觸發按鈕點擊時間後,如下圖:
1、字段為public的,
2、字段為internal的,
可以看出字段為internal後,綁定是沒有作用的。所以在綁定的時候數據模型類,必須為public類型才可以。