WP7: Why does a ListBox.ItemsPanel break my ElementName data binding?
Posted
by
iguanaNet
on Stack Overflow
See other posts from Stack Overflow
or by iguanaNet
Published on 2011-01-09T16:49:52Z
Indexed on
2011/01/09
16:53 UTC
Read the original article
Hit count: 197
I have a Windows Phone 7 ListBox
that binds to a list of integers. I am using the default MVVM Light template, so there is a ViewModel
class that contains data and a simple RelayCommand
. Here is the ListBox:
<ListBox ItemsSource="{Binding MyData}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding ElementName=ContentGrid, Path=DataContext.TestCommand}"
CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
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: new RelayCommand<int>(i => MessageBox.Show("Test" + i));
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.
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
I have tried some other types of binding for the EventToCommand
. For example, specifying my ViewModel
as a static resource. It works, but is less ideal than the example above.
Why does that ItemsPanel
break the databinding?
© Stack Overflow or respective owner