2014-05-21 66 views
0

我已經做了一個簡單的測試來更新用戶界面中的綁定值,但似乎沒有更新,只有初始值設置,但從未更新,我會失蹤?Windows商店8.1動態綁定不更新在用戶界面

代碼:

//the model class 

public class DemoCustomer : INotifyPropertyChanged 
{ 
    // These fields hold the values for the public properties. 
    private Guid idValue = Guid.NewGuid(); 
    private string customerNameValue = String.Empty; 
    private string phoneNumberValue = String.Empty; 

    public event PropertyChangedEventHandler PropertyChanged= delegate { }; 

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    // The constructor is private to enforce the factory pattern. 
    public DemoCustomer() 
    { 
     customerNameValue = "Customer"; 
     phoneNumberValue = "(312)555-0100"; 
    } 

    // This is the public factory method. 
    public static DemoCustomer CreateNewCustomer() 
    { 
     return new DemoCustomer(); 
    } 

    // This property represents an ID, suitable 
    // for use as a primary key in a database. 
    public Guid ID 
    { 
     get 
     { 
      return this.idValue; 
     } 
    } 

    public string CustomerName 
    { 
     get 
     { 
      return this.customerNameValue; 
     } 

     set 
     { 
      if (value != this.customerNameValue) 
      { 
       this.customerNameValue = value; 
       NotifyPropertyChanged(); 
      } 
     } 
    } 

    public string PhoneNumber 
    { 
     get 
     { 
      return this.phoneNumberValue; 
     } 

     set 
     { 
      if (value != this.phoneNumberValue) 
      { 
       this.phoneNumberValue = value; 
       NotifyPropertyChanged(); 
      } 
     } 
    } 
} 

然後,只需在我的主頁我這樣做:

public ObservableCollection<DemoCustomer> progcollection = new ObservableCollection<DemoCustomer>(); 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     progcollection = new ObservableCollection<DemoCustomer>(); 

     this.progcollection.Add(new DemoCustomer()); 
     this.txtblk.DataContext = progcollection[0].CustomerName; 



    } 
在點擊收聽例如

那麼我這樣做:

private void Button_Click_1(object sender, RoutedEventArgs e) 
    { 
     progcollection[0].CustomerName = "we changed the name!"; 
    } 

但沒有更新在用戶界面!

這裏是我的XAML:

<Page 
x:Class="downloadprogressbinding.MainPage" 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:simpledownload" 
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}"> 

    <TextBlock x:Name="txtblk" HorizontalAlignment="Left" Margin="994,421,0,0" TextWrapping="Wrap" Text="{Binding Mode=TwoWay}" VerticalAlignment="Top" Height="89" Width="226" FontSize="36"/> 

    <Button Content="Button" HorizontalAlignment="Left" Height="51" Margin="116,24,0,0" VerticalAlignment="Top" Width="407" Click="Button_Click_1"/> 

</Grid> 

回答

0

在結合和指定字段使用路徑關鍵字解決它,就像這樣:

{Binding Path=thetext, Mode=TwoWay} 
相關問題