0

I have been scratching my head on why the selection changed event for a listbox does not fire. I have a panaroma items being created dynamically in the code behind... Kind of new to wpf/xaml

<Style x:Key="PanoramaItemStyle" TargetType="ContentControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Grid>
                    <controls:PanoramaItem x:Name="ItemLocationPanoramaItem" Header="{Binding TagName}">
                        <ListBox  ItemsSource="{Binding ItemLocators}" Height="496" SelectedItem="{Binding SelectedItemLocation, Mode=TwoWay}" SelectionChanged="ItemLocatorsList_SelectionChanged" >
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate >
                                    <StackPanel Orientation="Vertical"  ScrollViewer.VerticalScrollBarVisibility="Auto" />
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                            <StackPanel Width="311">
                                                <TextBlock Text="{Binding Item.Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}"/>
                                                <TextBlock Text="{Binding Location.Description}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </controls:PanoramaItem>
                    <ContentPresenter/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="White"/>
</Style>

This is in the codebehind for the view.

 public LocationGroups()
    {
        InitializeComponent(); 
        LocationGroupsPanaroma.DefaultItem = LocationGroupsPanaroma.Items[0];
        viewModel = this.DataContext as LocationGroupsViewModel;
        CreateDynamicPanaromaItems();
    }


    private void CreateDynamicPanaromaItems()
    {
        foreach (Model.LocationGroup group in viewModel.LocationGroups)
        {
            if (group.TotalItems > 0)
            {
                PanoramaItem pi = new PanoramaItem();
                pi.Header = group.Name;
                pi.Orientation = System.Windows.Controls.Orientation.Horizontal;
                ItemLocationListViewModel itemLocationViewModel = viewModel[group.LocationGroupId];
                pi.DataContext = itemLocationViewModel;
                pi.Style = Resources["PanoramaItemStyle"] as Style; 
                LocationGroupsPanaroma.Items.Add(pi);
            }
        }

    }
AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70
pal Khoa
  • 9
  • 1
  • 6
  • You are going to have to provide a bit more information about what your problem actually is! You mention a ListBox SelectionChanged event is not firing, but your code snippets are all about populating a Panorama control. I would firstly, simplify your code until it is the simplest example that illustrates your problem, then update your question. – ColinE Jul 11 '11 at 07:19
  • What is your question? Your included code doesn't include anything that can be identified as definitely being a Listbox. There is also no reference to teh SelectionChanegd event. Ad why the `mvvm-light` tag? Again, there's nothing in the code which refers to it? – Matt Lacey Jul 11 '11 at 07:21
  • I wonder why the xaml in my original post did not show up – pal Khoa Jul 11 '11 at 14:21
  • Can someone edit my original post.. wonder why the xaml is not showing up. I checked the other posts..and that doesn't address this specific problem – pal Khoa Jul 12 '11 at 02:07
  • Code and Xml need to be prefixed with at last 4 spaces or 1 tab to be formatted as code. Your first line of your Xaml did not have any indentation, and therefore the following block was not trated as code and hidden. – AxelEckenberger Jul 14 '11 at 08:04
  • The code and the XAML do not match (different variable names etc.), also **what** are you trying to do? Furthermore, consider whether you **need** the code behind (see the answers in my post). – AxelEckenberger Jul 15 '11 at 05:13

2 Answers2

1

if you were using mvvm light you could do the following (fill your code in for the <> remarks:

<i:Interaction.Triggers>
  <i:EventTrigger EventName="SelectionChanged">
    <mvvm:EventToCommand 
    Command="{Binding <yourviewmodel>.<yourrelaycommand>, Mode=OneWay, Source={StaticResource Locator}}" CommandParameter="{Binding SelectedValue, ElementName=<nameofyourlistbox>}"/>
  </i:EventTrigger>

Didier Caron
  • 570
  • 3
  • 9
0

See my answers to this post and this post on how to dynamically generate panorama items and list boxes using mvvm, i.e. with no code behind.

Community
  • 1
  • 1
AxelEckenberger
  • 16,628
  • 3
  • 48
  • 70