Windows phone 7 双向绑定不起作用

Windows phone 7 双向绑定不起作用,windows-phone-7,data-binding,mvvm,silverlight-4.0,windows-phone-8,Windows Phone 7,Data Binding,Mvvm,Silverlight 4.0,Windows Phone 8,我不熟悉wp7中的双向绑定。下面的代码不会自动将textbox值分配给对象,并返回null Xaml: <Grid x:Name="ContentPanel" DataContext="{Binding usd}" Grid.Row="1" Margin="14,10,10,-10" > <TextBox Text="{Binding UserName,Mode=TwoWay}" Name="txt1" Width="200" Height="60" Fon

我不熟悉wp7中的双向绑定。下面的代码不会自动将textbox值分配给对象,并返回null

Xaml:

 <Grid x:Name="ContentPanel" DataContext="{Binding usd}" Grid.Row="1" Margin="14,10,10,-10" > 
        <TextBox Text="{Binding UserName,Mode=TwoWay}" Name="txt1" Width="200" Height="60" FontSize="20" Margin="128,48,128,499"/>
        <TextBox Text="{Binding Password,Mode=TwoWay}" Name="txt2" Width="200" Height="60" FontSize="20" Margin="128,263,128,284"/>
        <TextBox Text="{Binding Email,Mode=TwoWay}" Name="txt3" Width="200" Height="60" FontSize="20" Margin="128,159,128,388"/>
        <Button Content="Send" FontSize="18" Margin="179,413,170,129" 
        Click="Button_Click_1" />
  </Grid>
public class UserLogin:INotifyPropertyChanged
    {
        private string _username;
        private string _pwd;
        private string _email;

        public string UserName
        {
            get
            {
                return _username;
            }
            set
            {
                _username = value;
                OnPropertyChanged("UserName");
            }
        }
        public string Password
        {
            get
            {
                return _pwd;
            }
            set
            {
                _pwd = value;
                OnPropertyChanged("Password");
            }
        }
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                _email = value;
                OnPropertyChanged("Email");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
public UserLogin usd = null;
usd = new UserLogin();
    private void Button_Click_1(object sender, RoutedEventArgs e)
        {

//            ContentPanel.DataContext = usd;

            MessageBox.Show(usd.Email);
        }
实例化:

 <Grid x:Name="ContentPanel" DataContext="{Binding usd}" Grid.Row="1" Margin="14,10,10,-10" > 
        <TextBox Text="{Binding UserName,Mode=TwoWay}" Name="txt1" Width="200" Height="60" FontSize="20" Margin="128,48,128,499"/>
        <TextBox Text="{Binding Password,Mode=TwoWay}" Name="txt2" Width="200" Height="60" FontSize="20" Margin="128,263,128,284"/>
        <TextBox Text="{Binding Email,Mode=TwoWay}" Name="txt3" Width="200" Height="60" FontSize="20" Margin="128,159,128,388"/>
        <Button Content="Send" FontSize="18" Margin="179,413,170,129" 
        Click="Button_Click_1" />
  </Grid>
public class UserLogin:INotifyPropertyChanged
    {
        private string _username;
        private string _pwd;
        private string _email;

        public string UserName
        {
            get
            {
                return _username;
            }
            set
            {
                _username = value;
                OnPropertyChanged("UserName");
            }
        }
        public string Password
        {
            get
            {
                return _pwd;
            }
            set
            {
                _pwd = value;
                OnPropertyChanged("Password");
            }
        }
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                _email = value;
                OnPropertyChanged("Email");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
public UserLogin usd = null;
usd = new UserLogin();
    private void Button_Click_1(object sender, RoutedEventArgs e)
        {

//            ContentPanel.DataContext = usd;

            MessageBox.Show(usd.Email);
        }
在构造函数中:

 <Grid x:Name="ContentPanel" DataContext="{Binding usd}" Grid.Row="1" Margin="14,10,10,-10" > 
        <TextBox Text="{Binding UserName,Mode=TwoWay}" Name="txt1" Width="200" Height="60" FontSize="20" Margin="128,48,128,499"/>
        <TextBox Text="{Binding Password,Mode=TwoWay}" Name="txt2" Width="200" Height="60" FontSize="20" Margin="128,263,128,284"/>
        <TextBox Text="{Binding Email,Mode=TwoWay}" Name="txt3" Width="200" Height="60" FontSize="20" Margin="128,159,128,388"/>
        <Button Content="Send" FontSize="18" Margin="179,413,170,129" 
        Click="Button_Click_1" />
  </Grid>
public class UserLogin:INotifyPropertyChanged
    {
        private string _username;
        private string _pwd;
        private string _email;

        public string UserName
        {
            get
            {
                return _username;
            }
            set
            {
                _username = value;
                OnPropertyChanged("UserName");
            }
        }
        public string Password
        {
            get
            {
                return _pwd;
            }
            set
            {
                _pwd = value;
                OnPropertyChanged("Password");
            }
        }
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                _email = value;
                OnPropertyChanged("Email");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
public UserLogin usd = null;
usd = new UserLogin();
    private void Button_Click_1(object sender, RoutedEventArgs e)
        {

//            ContentPanel.DataContext = usd;

            MessageBox.Show(usd.Email);
        }
在按钮点击事件中:

 <Grid x:Name="ContentPanel" DataContext="{Binding usd}" Grid.Row="1" Margin="14,10,10,-10" > 
        <TextBox Text="{Binding UserName,Mode=TwoWay}" Name="txt1" Width="200" Height="60" FontSize="20" Margin="128,48,128,499"/>
        <TextBox Text="{Binding Password,Mode=TwoWay}" Name="txt2" Width="200" Height="60" FontSize="20" Margin="128,263,128,284"/>
        <TextBox Text="{Binding Email,Mode=TwoWay}" Name="txt3" Width="200" Height="60" FontSize="20" Margin="128,159,128,388"/>
        <Button Content="Send" FontSize="18" Margin="179,413,170,129" 
        Click="Button_Click_1" />
  </Grid>
public class UserLogin:INotifyPropertyChanged
    {
        private string _username;
        private string _pwd;
        private string _email;

        public string UserName
        {
            get
            {
                return _username;
            }
            set
            {
                _username = value;
                OnPropertyChanged("UserName");
            }
        }
        public string Password
        {
            get
            {
                return _pwd;
            }
            set
            {
                _pwd = value;
                OnPropertyChanged("Password");
            }
        }
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                _email = value;
                OnPropertyChanged("Email");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
public UserLogin usd = null;
usd = new UserLogin();
    private void Button_Click_1(object sender, RoutedEventArgs e)
        {

//            ContentPanel.DataContext = usd;

            MessageBox.Show(usd.Email);
        }

消息框语句中出现空引用异常。谢谢..

您可以只绑定到公共属性-因此您的:DataContext=“{Binding usd}”应该是错误的,因为usd只是一个字段

顺便说一句,如果你在你的ctor中也设置了这个,移除xaml绑定就可以了

usd = new UserLogin();
ContentPanel.DataContext = usd;
关于控件/页面(哪个XAML属于它)

  • 它的datacontext应该包含一个usd属性
  • 该属性还应通知属性
  • 当然,控件/页面的datacontext类也应该实现INotifyPropertyChanged

  • 因为你的美元没有被设定为一个属性,它只是一个变量。。。。做一件事

    public UserLogin usd {get;set;} 
    usd = null;
    

    太棒了你的建议很管用。但是为什么我们不能在xaml中设置数据绑定呢?因为您的美元没有设置为属性,它只是一个变量。。。。做一件事公共用户登录usd{get;set;}usd=null;