如果页面上有2个列表框,则不会触发列表框SelectionChanged事件

时间:2011-01-18 03:52:08

标签: xaml windows-phone-7

有没有人遇到过为什么如果页面上有两个列表框,SelectionChanged事件不会触发任何列表框?即使只有一个列表框,例如列表框下方的文本块,列表框SelectionChanged事件也不会触发。如果我删除第二个列表框或TextBlock(在第一个列表框下面),将触发第一个列表框SelectionChanged事件。我甚至尝试将第二个列表框放在自己的网格中,但仍然没有运气来获取任何选择更改的事件。任何想法或解决方法?谢谢!

嗨,抱歉没有发布代码,所以在这里。

<!--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>

另一个重要项目。我将列表框绑定到后面的代码中的通用字符串列表。谢谢!

2 个答案:

答案 0 :(得分:1)

一定是你正在做的事情,请出示你的代码。以下工作表明,可以将多个SelectionChanged事件连接到同一页面上的多个列表框

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);
}

<强>更新

如果我在你的xaml中使用以下内容,我仍然无法重新创建它。

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); 
}

使用上面的代码可以很好地处理XAML,事件处理程序会显示相应的消息。

请展示显示问题的完整示例。

答案 1 :(得分:1)

我的感觉是你可能会让你的控件重叠,以至于事件无法进入你想要的控件。

如果没有看到这种情况的重演,就很难提供更多的猜测。