ListBox SelectionChanged没有被调用 [英] ListBox SelectionChanged not getting called

查看:66
本文介绍了ListBox SelectionChanged没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的带有按钮的列表"框的代码.

The below is my code for List box with button.

我在这里有两个问题:

1)SelectionChanged不触发我获取所选项目及其值.

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

2)我的列表框用于选择多个项目,因此当我选择一个项目时,按钮上没有设置背景.

2) My list box is for multiple items selection, so when i select one item the background is not set on the 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>         

编辑

这是我的带有边框的文字块

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>

问题在这里:

1)如何更改选择项的背景颜色,我在XAML中设置了边框"背景.

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

2)如何添加多项选择.

2) How to add Multiple item selection.

推荐答案

我相信您的问题是由一个按钮组成的DataTemplate.一个按钮通常会处理路由的mouseclick事件,并且不会给列表框提供操作的灵活性,因此您不会收到选择事件.

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

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

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

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

这篇关于ListBox SelectionChanged没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆