Note that there are some explanatory texts on larger screens.

plurals
  1. PODataContext - ListView - Refresh UI - INotifyPropertyChanged
    primarykey
    data
    text
    <p>Working on an windows store app, I try to update/refresh a listView when some data is updated. But despite all samples and documentations I read, it doesn't work...</p> <p>Here my code behind : (I do not provide my xaml files, it's just a sample listView).</p> <p>So the class which implements the INotifyPropertyChanged interface:</p> <pre><code>public class Download : INotifyPropertyChanged { public enum DownloadState { Running, Waiting, Pausing, Paused, Cancelling, Cancelled }; private String Uri; private StorageFile storageFile; private String tempFileName; private String fileName; private String version ; private long totalSize ; private long downloadedBytes; private DownloadState state; private Protocol protocol; public Download(String Uri, StorageFile file, String fileName, String version, long totalSize, Protocol protocol) { this.Uri = Uri; this.storageFile = file; this.tempFileName = ""; this.fileName = fileName; this.version = version; this.totalSize = totalSize; this.downloadedBytes = 0; this.state = DownloadState.Waiting; this.protocol = protocol; } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { System.Diagnostics.Debug.WriteLine("Update!"); //ok if (PropertyChanged != null) { //PropertyChanged is always null and shouldn't. PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public DownloadState State { get{return this.state;} set { this.state = value; NotifyPropertyChanged(); } } //+some others methods } } </code></pre> <p>And the main page of the metro app : </p> <pre><code>// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237 namespace ClientAirNavLight_WS { /// &lt;summary&gt; /// A basic page that provides characteristics common to most applications. /// &lt;/summary&gt; public sealed partial class MainPage : ClientAirNavLight_WS.Common.LayoutAwarePage { /// &lt;summary&gt; /// Represent a Web Service proxy. /// &lt;/summary&gt; private AirNavLight_WSClientClient proxyWS; /// &lt;summary&gt; /// Initiialize the component of the application's main page. /// &lt;/summary&gt; public MainPage() { this.InitializeComponent(); } /// &lt;summary&gt; /// Populates the page with content passed during navigation. Any saved state is also /// provided when recreating a page from a prior session. /// &lt;/summary&gt; /// &lt;param name="navigationParameter"&gt;The parameter value passed to /// &lt;see cref="Frame.Navigate(Type, Object)"/&gt; when this page was initially requested. /// &lt;/param&gt; /// &lt;param name="pageState"&gt;A dictionary of state preserved by this page during an earlier /// session. This will be null the first time a page is visited.&lt;/param&gt; protected override void LoadState(Object navigationParameter, Dictionary&lt;String, Object&gt; pageState) { } /// &lt;summary&gt; /// Preserves state associated with this page in case the application is suspended or the /// page is discarded from the navigation cache. Values must conform to the serialization /// requirements of &lt;see cref="SuspensionManager.SessionState"/&gt;. /// &lt;/summary&gt; /// &lt;param name="pageState"&gt;An empty dictionary to be populated with serializable state.&lt;/param&gt; protected override void SaveState(Dictionary&lt;String, Object&gt; pageState) { } //Simulate data update. private async void Button_Click(object sender, RoutedEventArgs e) { object selected = this.ListResult.SelectedItem; if (selected != null) { //simulate an update in data. // ListView should be refresh in order to reflect changes made here. Download dl = (Download)selected; if (dl.State == Download.DownloadState.Paused) { dl.State = Download.DownloadState.Running; } else { dl.State = Download.DownloadState.Paused; } } else { //Just add an item to the list view. StorageFile file = await this.getStorageFile("test"); Download dl = new Download("192.128.2.14", file, "test", "1.2", 100, Protocol.HTTP); this.ListResult.Items.Add(dl); this.ListResult.DataContext = dl;// Does not work. } } private async Task&lt;StorageFile&gt; getStorageFile(String suggestedFileName) { FileSavePicker savePicker = new FileSavePicker(); savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; // Dropdown of file types the user can save the file as savePicker.FileTypeChoices.Add("Application/pdf", new List&lt;string&gt;() { ".pdf" }); savePicker.FileTypeChoices.Add("Archive", new List&lt;string&gt;() { ".zip", ".rar", ".7z" }); savePicker.FileTypeChoices.Add("Plain-text", new List&lt;string&gt;() { ".txt" }); // Default file name if the user does not type one in or select a file to replace savePicker.SuggestedFileName = suggestedFileName; return await savePicker.PickSaveFileAsync(); } } } </code></pre> <p>So How am I supposed to use listView.DataContext? Am I misunderstanding how use INotifyPropertyChanged?</p> <p><strong>EDIT :</strong></p> <p>How my listView is define : </p> <pre><code> &lt;ListView x:Name="ListResult" HorizontalAlignment="Left" Height="200" Margin="10,56,0,0" VerticalAlignment="Top" Width="653" SelectionMode="Single"/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload