2014-07-25 132 views
2

我有一個主要問題,我的數據從TextBox綁定到ViewModel到TextBlock。我已經建立了我的下面XAML代碼如下所示:數據綁定問題

<Page 
x:Class="digiBottle.MainPage" 
DataContext="{Binding Source=UserProfile}" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:digiBottle" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d"> 

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" 
     DataContext="{Binding Source=UserProfile}"> 

    <TextBlock HorizontalAlignment="Left" Margin="219,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="32" Width="232" Text="{Binding userFirstName, Mode=OneWay}"/> 
    <TextBox HorizontalAlignment="Left" Margin="39,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="111" Text="{Binding userFirstName, UpdateSourceTrigger=PropertyChanged}"/> 

</Grid> 

的.cs文件我想如下爲源被定義爲使用方法:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace digiBottle.Model 

{ 公共類UserProfile:INotifyPropertyChanged {

public int ID { get; set; } 
    public string userFirstName; 
    public string userLastName { get; set; } 
    public int age { get; set; } 
    public int weight { get; set; } 
    public int height { get; set; } 
    public DateTime signupTime { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public UserProfile() 
    { 
     userFirstName = "First Name"; 
    } 


    private void RaisePropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

    } 

    public UserProfile getCopy() 
    { 
     UserProfile copy = (UserProfile)this.MemberwiseClone(); 
     return copy; 
     } 
    } 
} 

當我試圖將我的TextBox和TextBlock綁定到UserProfile.cs源中的userFirstName時,我做錯了什麼。任何幫助將是一個主要幫助!

謝謝

+0

那是一個Windows Phone的問題或Windows 8? – Adil

回答

1

我在這裏注意到的第一件事是,你的屬性(setter方法)沒有提高事件的變化。你需要在你的屬性設置器中調用RaisePropertyChanged。

我會寫它像

私有字段

private String _userFirstName; 

然後在構造函數中

public UserProfile() 
{ 
     this._userFirstName = "First Name"; 
} 

隨着房產籌集事件

public String UserFirstName 
{ 
    get { return this._userFirstName; } 
    set 
    { 
     this._userFirstName = value; 
     this.RaisePropertyChanged("UserFirstName"); 
    } 
} 

然後在XAML,將其綁定到具有雙向綁定的屬性「UserFirstName」

<TextBlock HorizontalAlignment="Left" Margin="219,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="32" Width="232" Text="{Binding UserFirstName, Mode=OneWay}"/> 
<TextBox HorizontalAlignment="Left" Margin="39,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="111" Text="{Binding UserFirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 
+0

這個答案也可以。 :D –

+0

這兩個偉大的建議。感謝你們! – RupaRupa

1

DataBinding起初很難理解。請參考Data binding for Windows Phone 8開始。

爲您的代碼:這裏有修復你將需要:

  1. 記住,你只能綁定一個屬性。
  2. 您需要在設定的動作上提升事件。
  3. 根據您想要的操作,您可能需要雙向綁定文本框。
  4. 您需要爲textbox和textblock設置DataContext。

下面是變化:

CS

public class UserProfile : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    string user_first_name; 
    public String UserFirstName 
    { 
     get { return user_first_name; } 
     set 
     { 
      user_first_name = value; 
      OnPropertyChanged("UserFirstName"); 
     } 
    } 

    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 

} 


public partial class MainPage : PhoneApplicationPage 
{ 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 

     UserProfile up = new UserProfile(); 
     this.tb1.DataContext = up; 
     this.tb2.DataContext = up; 
    } 
} 

XAML

<TextBlock x:Name="tb2" TextWrapping="Wrap" Text="{Binding UserFirstName}"/> 
<TextBox x:Name="tb1" HorizontalAlignment="Left" Height="72" Margin="14,475,0,0" Grid.Row="1" TextWrapping="Wrap" Text="{Binding UserFirstName, Mode=TwoWay}" VerticalAlignment="Top" Width="456" /> 
+0

哇,非常感謝你,將此代碼用於試駕。它將解決我所有的問題!你驚人的! – RupaRupa