Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListBox SelectionChanged未被调用_C#_Wpf_Windows_Windows Phone 8_Listbox - Fatal编程技术网

C# ListBox SelectionChanged未被调用

C# ListBox SelectionChanged未被调用,c#,wpf,windows,windows-phone-8,listbox,C#,Wpf,Windows,Windows Phone 8,Listbox,下面是我的按钮列表框代码 我这里有两个问题: 1) SelectionChanged不触发以获取所选项目及其值 2) 我的列表框用于选择多个项目,因此当我选择一个项目时,按钮上不会设置背景 如何解决这些问题 <ListBox Name="listBox" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" SelectionChanged=

下面是我的按钮列表框代码

我这里有两个问题:

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

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

如何解决这些问题

<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) 如何更改选择项的背景色,我已经在XAML中设置了边框背景


2) 如何添加多个项目选择。

我认为您的问题在于包含按钮的数据模板。按钮通常会处理路由的mouseclick事件,并且不会为Listbox提供对其进行操作的操作,因此您不会获得选择事件

例如,尝试将您的按钮元素更改为边框,然后查看您的事件是否触发

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

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


是完全不发射还是仅在第二次尝试时发射?改为使用
轻触
:)@ToniPetrina它不会向目标开火all@ToniPetrina你能给我举个例子吗?请同时查看我的编辑是的,我已尝试仅使用边框,并且我的选择更改事件正在触发。请查看我的编辑