2014-05-21 68 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} 
相关问题