ListBox SelectionChanged not getting called

2019-08-30 03:08发布

问题:

The below is my code for List box with button.

I have two issues here:

1) SelectionChanged not firing for me to get the selected item and its value.

2) My list box is for multiple items selection, so when i select one item the background is not set on the button.

How to solve these issues ?

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

EDIT

Here my text block with border

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

Issues here:

1) How to change the selection item background color, i have set Border background in XAML.

2) How to add Multiple item selection.

回答1:

I believe that your problem is your DataTemplate which consists of a button. A button will normaly handled the routed mouseclick event and will not give the Listbox an opertunity to act on it, therefore your not getting a selection event.

Try to change your button element to a border for example and see if your event is fireing then?

It might also not be a bad idea to use an attached property and bind the selection change to a command,

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

after you reference the xmlns you could use the code in xaml like so:

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