Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reference ObservableCollection from WPF ListView SelectionChanged event handler?
    text
    copied!<p>In WPF app I have a ListView which is connected with ObservableCollection <code> ShQuCollection</code> through databinding:</p> <pre><code>&lt;ListView Name="ShSelList" ItemsSource="{Binding Source={StaticResource myDataSource},Path=ShQuCollection}" SelectionChanged="ShSelList_SelectionChanged"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="Code" DisplayMemberBinding="{Binding StrCode}"/&gt; &lt;GridViewColumn Header="Date" DisplayMemberBinding="{Binding Date}"/&gt; &lt;GridViewColumn Header="Time" DisplayMemberBinding="{Binding Time}"/&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>From inside ListView SelectionChanged event handler I need to call a method and pass to it a string parameter, taking it from one of the field of the selected row of the ObservableCollection <code> ShQuCollection</code>.</p> <p>How I could reference the ObservableCollection from inside ListView SelectionChanged event handler?</p> <pre><code>private void ShSelList_SelectionChanged(object sender, SelectionChangedEventArgs e) { ...????? } </code></pre> <p><strong>Edited (added):</strong></p> <p>My ObservableCollection is in code-behind file of another window and I use <code>Window.Resources</code> declaration to reach it.</p> <pre><code>&lt;Window.Resources&gt; &lt;c:ShWindow x:Key="myDataSource"/&gt; &lt;/Window.Resources&gt; </code></pre> <p>And ObservableCollection looks like:</p> <pre><code> ObservableCollection&lt;ShsQu&gt; _ShQuCollection = new ObservableCollection&lt;ShsQu&gt;(); public ObservableCollection&lt;ShsQu&gt; ShQuCollection { get { return _ShQuCollection; } } public class ShsQu { public string StrCode { get; set; } public string Date { get; set; } public string Time { get; set; } } </code></pre>
 

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