Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting ID of button clicked inside itemSource?
    primarykey
    data
    text
    <p>I have an observableCollection that I loop through using an itemSource inside my view. Each item in the observablecollection contains a button that sends a command (openSessionCommand) to the viewModel. My question is - how can I send and ID back to the viewModel of which button was clicked inside the itemSource?</p> <pre><code>&lt;ItemsControl ItemsSource="{Binding AvailableSessions}" Margin="490,181,10.111,39.111"&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate &gt; &lt;Border BorderBrush="Black" Background="Gainsboro" BorderThickness="1" Margin="2"&gt; &lt;Grid Background="#FFECECEC"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.OpenSessionCommand}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="243" Height="42"&gt; &lt;TextBlock TextWrapping="Wrap"&gt; &lt;Run Text="{Binding SessionName}"/&gt;&lt;LineBreak/&gt; &lt;Run Text="{Binding Genre}"/&gt;&lt;Run Text=" - "/&gt;&lt;Run Text="{Binding Tempo}"/&gt; &lt;/TextBlock&gt; &lt;/Button&gt; &lt;Label Content="{Binding AdminUsername}" HorizontalAlignment="Left" Margin="10,53,0,0" VerticalAlignment="Top" Width="243" Height="26"/&gt; &lt;Label Content="{Binding Client1Username}" HorizontalAlignment="Left" Margin="10,71,0,0" VerticalAlignment="Top" Width="243" Height="25"/&gt; &lt;/Grid&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;ItemsControl.Template&gt; &lt;ControlTemplate TargetType="ItemsControl"&gt; &lt;ScrollViewer CanContentScroll="True"&gt; &lt;ItemsPresenter/&gt; &lt;/ScrollViewer&gt; &lt;/ControlTemplate&gt; &lt;/ItemsControl.Template&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;VirtualizingStackPanel/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; </code></pre> <p></p> <p>DelegateCommand:</p> <pre><code>public class DelegateCommand : ICommand { private readonly Action _command; private readonly Func&lt;bool&gt; _canExecute; public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public DelegateCommand(Action command, Func&lt;bool&gt; canExecute = null) { if (command == null) throw new ArgumentNullException(); _canExecute = canExecute; _command = command; } public void Execute(object parameter) { _command(); } public bool CanExecute(object parameter) { return _canExecute == null || _canExecute(); } } </code></pre> <p>ICommand:</p> <pre><code>public ICommand OpenSessionCommand { get { return new DelegateCommand(OpenSession); } } public void OpenSession() { ContinueReceiving = false; dispatcherTimer.Stop(); Messenger.Default.Send&lt;NavigateMessage&gt;( new NavigateMessage(SessionViewModel.ViewName, this)); } </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.
 

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