Note that there are some explanatory texts on larger screens.

plurals
  1. POWP7: Why does a ListBox.ItemsPanel break my ElementName data binding?
    primarykey
    data
    text
    <p>I have a Windows Phone 7 <code>ListBox</code> that binds to a list of integers. I am using the default MVVM Light template, so there is a <code>ViewModel</code> class that contains data and a simple <code>RelayCommand</code>. Here is the ListBox:</p> <pre><code>&lt;ListBox ItemsSource="{Binding MyData}"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Button Content="{Binding}"&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="Click"&gt; &lt;cmd:EventToCommand Command="{Binding ElementName=ContentGrid, Path=DataContext.TestCommand}" CommandParameter="{Binding}" /&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/Button&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>This displays a vertical list of integers inside buttons. If you click any of them, the following command code executes and shows a pop-up: <code>new RelayCommand&lt;int&gt;(i =&gt; MessageBox.Show("Test" + i));</code></p> <p>However, if I simply add the following XAML to change to a horizontal list, the databinding fails. Nothing happens when you click the button and no error messages are written to the Output window.</p> <pre><code>&lt;ListBox.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;StackPanel Orientation="Horizontal" HorizontalAlignment="Center" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListBox.ItemsPanel&gt; </code></pre> <p>I have tried some other types of binding for the <code>EventToCommand</code>. For example, specifying my <code>ViewModel</code> as a static resource. It works, but is less ideal than the example above.</p> <p>Why does that <code>ItemsPanel</code> break the databinding?</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.
 

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