程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> WPF-14:綁定中數據模型必須為public問題

WPF-14:綁定中數據模型必須為public問題

編輯:C#入門知識

不久前遇到一個問題,在綁定的時候打算將數據模型類全部設置為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類型才可以。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved