WPF 列表框选择更改不会在项目单击时触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15217099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:52:35  来源:igfitidea点击:

WPF Listbox Selection change not firing on Item click

wpflistboxwpf-controlslistboxitem

提问by Hardik

In my wpf application when I select listboxItem, SelectionChanged event of listbox is not firing. However event fires when I click on outer margin. have a look at below snap.

在我的 wpf 应用程序中,当我选择 listboxItem 时,不会触发列表框的 SelectionChanged 事件。但是,当我单击外边距时会触发事件。看看下面的快照。

enter image description here

在此处输入图片说明

so basically, when I click on section inside Red border(Right Image), Selection change event is not firing, but when I click on outer border (White color part), selection change fires.

所以基本上,当我点击红色边框(右图)内的部分时,选择更改事件不会触发,但是当我点击外边框(白色部分)时,选择更改会触发。

While searching about issue, I am not sure but I found it may be issue due to event tunneling. However I have only a bit of knowledge about tunneling yet.

在搜索问题时,我不确定,但我发现它可能是由于事件隧道造成的问题。但是,我对隧道的了解还很少。

So can any one please help me how this can get working so that selection change fire when I click on listboxitem (Red section)

那么任何人都可以帮助我如何工作,以便当我单击 listboxitem(红色部分)时选择更改火

let me if I need to further clear question. I am also putting listbox code here

如果我需要进一步明确问题,请让我。我也把列表框代码放在这里

<ListBox x:Name="Listbox1" SelectionChanged="listBox1_SelectionChanged">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <ListBoxItem Margin="10" Content="{Binding Name}" Height="25" 
                                             BorderBrush="#FF404040" BorderThickness="0,0.25" />
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

Thanks in anticipation

感谢期待

回答by bitbonk

I can't think of a reason why you would want to have a ListBoxIteminside the DataTemplateof a ItemTemplate. The ListBoxItems are genarated automatically for each element of the ListBoxand whatever you have in your DataTemplate will be used as the contentof that ListBoxItemSo in your case you end up having a ListBoxIteminside a ListBoxItem. This might be the cause.

我想不出有任何理由,你为什么会想有一个ListBoxItem内部DataTemplateItemTemplate。该ListBoxItems的对的每个元素自动genarated ListBox,你有什么在你的DataTemplate将被用作内容说的ListBoxItem所以你的情况,你最终有ListBoxItem内部的ListBoxItem。这可能是原因。

Try it this way:

试试这个方法:

<ListBox x:Name="Listbox1" SelectionChanged="listBox1_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Label Margin="10" 
                   Content="{Binding Name}" 
                   Height="25"
                   BorderBrush="#FF404040" 
                   BorderThickness="0,0.25" />
         </DataTemplate>                        
    </ListBox.ItemTemplate>
</ListBox>