Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update ListView when existent items of bound list has been updated
    primarykey
    data
    text
    <p>I need to update the list of downloads when the progress has been changed. </p> <p>XAML:</p> <pre><code>&lt;ListView ItemsSource="{Binding UiList}" x:Name="MyListView"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="Title"/&gt; &lt;GridViewColumn Header="Progress"/&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>ViewModel that creates an instance of Logic class and updates the content of ListView:</p> <pre><code>class MainWindowViewModel : ViewModelBase { private readonly Logic _logic; public List&lt;Model&gt; UiList { get; set; } public MainWindowViewModel() { _logic = new Logic(); _logic.Update += LogicUpdate; Start = new RelayCommand(() =&gt; { var worker = new BackgroundWorker(); worker.DoWork += (sender, args) =&gt; _logic.Start(); worker.RunWorkerAsync(); }); } void LogicUpdate(object sender, EventArgs e) { UiList = _logic.List; RaisePropertyChanged("UiList"); } public ICommand Start { get; set; } } </code></pre> <p>Logic:</p> <pre><code>public class Logic { readonly List&lt;Model&gt; _list = new List&lt;Model&gt;(); public event EventHandler Update; public List&lt;Model&gt; List { get { return _list; } } public void Start() { for (int i = 0; i &lt; 100; i++) { _list.Clear(); _list.Add(new Model{Progress = i, Title = "title1"}); _list.Add(new Model { Progress = i, Title = "title2" }); var time = DateTime.Now.AddSeconds(2); while (time &gt; DateTime.Now) { } Update(this, EventArgs.Empty); } } } </code></pre> <p>The code above would not update UI. I know two way how to fix this:</p> <ol> <li><p>In xaml codebehind call: <code>Application.Current.Dispatcher.Invoke(new Action(() =&gt; MyListView.Items.Refresh()));</code></p></li> <li><p>In ViewModel change <code>List&lt;&gt;</code> to <code>ICollectionView</code> and use <code>Application.Current.Dispatcher.Invoke(new Action(() =&gt; UiList.Refresh()));</code> after the list has been updated. </p></li> </ol> <p>Both ways cause the problem: the ListView <strong>blinks</strong> and <strong>Popup</strong> that should be open on user demand always <strong>closes after each "refresh"</strong>:</p> <p><img src="https://i.stack.imgur.com/MaGOJ.png" alt="Popup"></p> <pre><code>&lt;Popup Name="Menu" StaysOpen="False"&gt; </code></pre> <p>I can replace the popup with another control or panel, but I need its possibility to be out of main window's border (like on screen). But I believe that WPF has another way to update the content of ListView (without blinks).</p> <p>PS: Sorry for long question, but I do not know how to describe it more briefly...</p>
    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.
 

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