Note that there are some explanatory texts on larger screens.

plurals
  1. POc# How to get Listbox selection from Observable Collection
    primarykey
    data
    text
    <p>I'm probably not even asking this correctly, I am new to c#, but trying to help my 14 year-old son learn. I've created a listbox with items created with an ObservableCollection. Here is the XAML:</p> <pre><code>&lt;ListBox x:Name="listBox1" ItemsSource="{Binding}" Margin="105,205,886,63" IsTabStop="True" SelectionChanged="PrintText" ScrollViewer.VerticalScrollBarVisibility="Hidden" TabIndex="5" FontSize="36" Background="Transparent" Foreground="#FF55B64C" FontFamily="Arabic Typesetting" FontWeight="Bold" IsDoubleTapEnabled="False" SelectionMode="Single" &gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Name="blockNameList" Text="{Binding name}"/&gt; &lt;TextBlock Text=" #"/&gt; &lt;TextBlock Name="blockIdList" Text="{Binding id}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>Here is how I created the ListBox Items:</p> <pre><code>var client = new HttpClient(); var uri = new Uri("http://theurlImusing"); Stream respStream2 = await client.GetStreamAsync(uri); // DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(RootObject)); // RootObject feed2 = (RootObject)ser2.ReadObject(respStream2); DataContractJsonSerializer ser = null; ser = new DataContractJsonSerializer(typeof(ObservableCollection&lt;RootObject&gt;)); ObservableCollection&lt;RootObject&gt; feed2 = ser.ReadObject(respStream2) as ObservableCollection&lt;RootObject&gt;; var cardList = new List&lt;RootObject&gt;(); foreach (RootObject returnfeed in feed2) { string cid = returnfeed.id; string cardname = returnfeed.name; listBox1.Items.Add(new RootObject { id=cid, name=cardname }); } </code></pre> <p>I thought I would just use the SelectionChanged="PrintText" property of the listbox so that when I clicked on a listbox item, it would just change a textblock's text value. Ultimately, that is all I am trying to do...set a textblock or textbox to be equal to the "id" value that is clicked on in the ListBox.</p> <pre><code>void PrintText(object sender, SelectionChangedEventArgs args) { //What do I put in here?? } </code></pre> <p>Thanks very much for any insight! I need it!!</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.
    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