Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks, Andrew, that did it. The key was that I had to create a separate DataContext for the DockPanel in question, and then bound it to a new instance of the Pledge object created higher up in the template.</p> <pre><code>&lt;ListBox&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;local:Pledge x:Key="pledge" /&gt; &lt;/Grid.Resources&gt; &lt;DockPanel DataContext="{Binding Source={StaticResource pledge}}" &gt; &lt;TextBox Name="txtAmount" Text="{Binding Path=Amount}" /&gt; &lt;TextBox Name="txtEnvelopeID" Text="{Binding Path=EnvelopeID}" /&gt; &lt;!-- Bind the Tag property of the Button to the DataContext of the ListBoxItem, i.e. the current Family object --&gt; &lt;Button Tag="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}" Name="addPledge" Click="addPledge_Click" &gt; Add Pledge --&gt; &lt;/Button&gt; &lt;/DockPanel&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>I still needed to know more details to setup the Pledge object appropriately, but I was able to get them by binding the Button's Tag property to the DataContext of its templated parent:</p> <pre><code>&lt;Button Tag="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}" Name="addPledge" Click="addPledge_Click" /&gt; </code></pre> <p>And from there, I was able to handle the rest in code-behind:</p> <pre><code> private void addPledge_Click(object sender, RoutedEventArgs e) { try { // Set any additional necessary properties. Button button = (Button)sender; Pledge pledge = (Pledge)button.DataContext; pledge.PledgeYear = (PledgeYear)cboYear.SelectedItem; pledge.Family = (Family)button.Tag; // Update the database and refresh the Listboxes. entities.AddToPledge(pledge); entities.SaveChanges(); LoadUnpledgedListBox(); LoadPledgedListBox(); } catch (System.Data.UpdateException ex) { MessageBox.Show("Error updating database: " + ex.Message); } catch (System.Exception ex) { Classes.Utils.HandleGenericError(ex); } } </code></pre> <p>Many thanks -- that pointed me in the right direction.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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