1

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

在此处输入图像描述

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

在搜索问题时,我不确定,但我发现它可能是由于事件隧道导致的问题。但是,我对隧道技术只有一点了解。

所以任何人都可以帮助我如何让它工作,以便当我点击 listboxitem(红色部分)时选择会改变

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

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

感谢期待

4

1 回答 1

4

我想不出你为什么想要 a 的ListBoxItem内部的DataTemplate原因ItemTemplate。s 会为的ListBoxItem每个元素自动生成,ListBox并且您在 DataTemplate 中拥有的任何内容都将用作 that 的内容ListBoxItem因此在您的情况下,您最终会拥有 aListBoxItem内部ListBoxItem。这可能是原因。

试试这种方式:

<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>
于 2013-03-05T06:29:54.727 回答