Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding a Button to a RelayCommand within an ItemsControl container
    text
    copied!<p>In a WPF Application, I have a ViewModel that is exposing a collection of strings that I'm displaying as buttons through an ItemsControl container using a WrapPanel. I haven't been able to bind the RelayCommand in my ViewModel to the buttons, however.</p> <p>ViewModel (IncidentAddressesViewModel):</p> <pre><code>public IEnumerable&lt;string&gt; Addresses { get; set; } public RelayCommand&lt;string&gt; ZoomToAddressCommand { get { if (this.zoomToAddressCommand == null) this.zoomToAddressComamnd = new RelayCommand&lt;string&gt;(this.ZoomToAddress); return this.zoomToAddressCommand; }} private void ZoomToAddress(string address) { MessageBox.Show (address); } </code></pre> <p>XAML:</p> <pre><code>&lt;TabItem x:Name="IncidentAddressesTab"&gt; &lt;ItemsControl ItemsSource="{Binding Addresses}"&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="Command"&gt; &lt;cmd:EventToCommand Command="{Binding ZoomToAddressCommand}" CommandParameter="{Binding Text}" PassEventArgsToCommand="True" /&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;WrapPanel Orientation="Vertical"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Button Content="{Binding}" /&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/TabItem&gt; </code></pre> <p>Xaml Code Behind that connects the DataContext</p> <pre><code>IncidentAddressesTab.DataContext = new IncidentAddressesViewModel(); </code></pre> <p>The buttons are showing with the addresses. When I set a breakpoint at the ZoomToAddressCommand, it does get hit once, but when I click the buttons, the ZoomToAddress method never gets invoked.</p> <p>UPDATE to include binding details: I am actually binding to the TabItem. I have updated the XAML to include the additional tag and added the binding code in the XAML Code Behind. I didn't know this was pertinent information or I would have added it in the beginning.. (:</p>
 

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