当前位置:编程学习 > C#/ASP.NET >>

WPF-14:绑定中数据模型必须为public问题

不久前遇到一个问题,在绑定的时候打算将数据模型类全部设置为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=&qu

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,