ListBox SelectionChanged没有被调用

下面是我的代码列表框与button。

我在这里有两个问题:

1)SelectionChanged没有射击我得到选定的项目和它的价值。

2)我的列表框是用于多个项目的select,所以当我select一个项目的背景没有设置在button上。

如何解决这些问题?

<ListBox Name="listBox" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" SelectionChanged="TopicListboxSelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Disabled"> <ListBox.ItemTemplate> <DataTemplate> <Button Name="AnswerCell" Width="456" Content="{Binding Path=Value}" Background="#FFF2F4F7" Foreground="Black" Style="{StaticResource CellStyle}"> <Button.ContentTemplate> <DataTemplate> <TextBlock Style="{StaticResource TextStyle}" Padding="0,20,0,20" TextAlignment="Center" Text="{Binding}"/> </DataTemplate> </Button.ContentTemplate> </Button> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

编辑

在这里我的文本块与边界

 <ListBox.ItemTemplate> <DataTemplate> <StackPanel Name="AnswerCellBack" Margin="0,0,0,4" Orientation="Horizontal"> <Border Name="borderColor" Background="#FFF2F4F7"> <TextBlock Name="Answertext" Width="456" Padding="10,20,10,20" TextAlignment="Center" Text="{Binding Path=AnswerValue}" Style="{StaticResource AnswerTextStyle}"/> </Border> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> 

问题在这里:

1)如何更改select项目的背景颜色,我已经在XAML中设置了边框背景。

2)如何添加多项select。

Solutions Collecting From Web of "ListBox SelectionChanged没有被调用"

我相信你的问题是你的DataTemplate由一个按钮组成。 一个按钮将正常处理路由的鼠标点击事件,并不会给列表框一个运行的行为,因此你没有得到一个选择事件。

尝试改变你的按钮元素的边界为例,看看你的事件是否正在焚烧呢?

使用附加属性并将选择更改绑定到命令也可能不是一个坏主意,

  public class SelectionChangeCommand : DependencyObject { public static bool GetIsRegistered(DependencyObject obj) { return (bool)obj.GetValue(IsRegisteredProperty); } public static void SetIsRegistered(DependencyObject obj, bool value) { obj.SetValue(IsRegisteredProperty, value); } // Using a DependencyProperty as the backing store for IsRegistered. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsRegisteredProperty = DependencyProperty.RegisterAttached("IsRegistered", typeof(bool), typeof(SelectionChangeCommand), new PropertyMetadata(false, new PropertyChangedCallback(RegisterForCommand))); private static void RegisterForCommand(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is Selector) { Selector sel = (Selector)d; if ((bool)e.NewValue) { sel.SelectionChanged += sel_SelectionChanged; } else { sel.SelectionChanged -= sel_SelectionChanged; } } } static void sel_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (sender is Selector) { Selector sel = (Selector)sender; ICommand command = GetCommand(sel); if (command!=null && command.CanExecute(null)) command.Execute(sel); } } public static ICommand GetCommand(DependencyObject obj) { return (ICommand)obj.GetValue(CommandProperty); } public static void SetCommand(DependencyObject obj, ICommand value) { obj.SetValue(CommandProperty, value); } // Using a DependencyProperty as the backing store for Command. This enables animation, styling, binding, etc... public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(SelectionChangeCommand), new PropertyMetadata(null)); } 

在引用xmlns之后,您可以像这样使用xaml中的代码:

  <ListBox kernelAttached:SelectionChangeCommand.Command="{Binding SelectedLinkCommand}" kernelAttached:SelectionChangeCommand.IsRegistered="True" >