sleeprince
sleeprince

Reputation: 13

Listbox SelectionChanged event does not fire if there are 2 listboxes on the page

Has anyone come across why if there are two listboxes on a page the SelectionChanged event does not fire for either listbox? Even if there is just one listbox and, say, a Textblock below the listbox the listbox SelectionChanged event wont fire. If I remove the second listbox or the TextBlock (below the first listbox) the first listbox SelectionChanged event will fire. I even tried putting the second listbox in its own grid and still no luck on getting any selectionchanged events firing. Any ideas or workarounds? Thanks!

HI, sorry for not posting code so here it is.

<!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox  Name="lbProps" Width="441" SelectionChanged="lbProps_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Name="spMain">
                            <Border BorderThickness="1" BorderBrush="white" >
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Text="{Binding}"  Margin="5" Width="430"  FontSize="22"/>
                                </StackPanel>
                            </Border>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
        <Grid x:Name="ContentPanel2" Grid.Row="2" Margin="12,0,12,0">
                <ListBox  Name="lbProp3" Width="441" SelectionChanged="lbProp3_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Name="spMain3">
                            <Border BorderThickness="1" BorderBrush="white" >
                                <StackPanel Orientation="Vertical">
                                   <TextBlock Text="{Binding}"  Margin="5" Width="430"  FontSize="22"/>
                               </StackPanel>
                            </Border>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Grid>

One other important item. I am binding the listboxes to a generic list of strings in the code behind. Thanks!

Upvotes: 0

Views: 3281

Answers (2)

Matt Lacey
Matt Lacey

Reputation: 65564

It must be something you're doing, please show your code. The following works and shows that it is possible to have multiple SelectionChanged events hooked up to multiple list boxes on the same page

xaml:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <StackPanel>
        <ListBox SelectionChanged="ListBox1_SelectionChanged">
            <ListBoxItem Content="1 - A" />
            <ListBoxItem Content="1 - B" />
            <ListBoxItem Content="1 - C" />
            <ListBoxItem Content="1 - D" />
            <ListBoxItem Content="1 - E" />
            <ListBoxItem Content="1 - F" />
        </ListBox>

        <TextBlock Text="some text" />

        <ListBox SelectionChanged="ListBox2_SelectionChanged">
            <ListBoxItem Content="2 - A" />
            <ListBoxItem Content="2 - B" />
            <ListBoxItem Content="2 - C" />
            <ListBoxItem Content="2 - D" />
            <ListBoxItem Content="2 - E" />
            <ListBoxItem Content="2 - F" />
        </ListBox>
    </StackPanel>
</Grid>

cs:

private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show((e.AddedItems[0] as ListBoxItem).Content.ToString(), 
                    "List 1", MessageBoxButton.OK);
}

private void ListBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show((e.AddedItems[0] as ListBoxItem).Content.ToString(),
                    "List 2", MessageBoxButton.OK);
}

Update

If I use the following with your xaml I still can't recreate this.

public MainPage()
{
    InitializeComponent();

    lbProps.ItemsSource = new ObservableCollection<String> { "one", "two", "three" };

    lbProp3.ItemsSource = new ObservableCollection<String> { "aaa", "bbb", "cccc" };
}

private void lbProps_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show(e.AddedItems[0].ToString(), "List 1", MessageBoxButton.OK); 
}

private void lbProp3_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show(e.AddedItems[0].ToString(), "List 2", MessageBoxButton.OK); 
}

Using the above code works fine with your XAML and the event handlers show the appropriate messages.

Please show a complete example which shows the problem.

Upvotes: 1

Mick N
Mick N

Reputation: 14882

My feeling was you may have your controls overlaping in such a way that events aren't getting to the controls you want.

Without seeing a repro of this it's difficult to offer more than speculation.

Upvotes: 1

Related Questions