簡體   English   中英

Windows Store 8.1動態綁定未在UI中更新

[英]Windows store 8.1 dynamic binding not updating in UI

我已經做了一個簡單的測試,用於更新UI中的綁定值,但是似乎沒有任何更新,僅設置了初始值,但從未更新,我會丟失什么?

碼:

 //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!";
    }

但是UI中沒有任何更新!!!

這是我的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>

在綁定中使用path關鍵字並指定字段即可解決該問題,如下所示:

{Binding Path=thetext, Mode=TwoWay}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM