gpt4 book ai didi

wpf - 使用 ItemTemplate 在 ListBoxItems 构建上添加事件

转载 作者:行者123 更新时间:2023-12-01 02:54:12 24 4
gpt4 key购买 nike

我有一个 ListBox像这样 :

    <ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
ListBoxItem.Selected="ListBoxItem_Selected">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<DockPanel>
<Label Content="{Binding Path=Attribute[rdv].Value, UpdateSourceTrigger=PropertyChanged}" />
</DockPanel>
<DockPanel>
<Label Content="{Binding Path=Attribute[type].Value, UpdateSourceTrigger=PropertyChanged}" />
<Label Content="{Binding Path=Element[ville].Attribute[saisie].Value, UpdateSourceTrigger=PropertyChanged}" />
<Label Content=":" />
<Label Content="{Binding Path=Element[adresse].Attribute[saisie].Value, UpdateSourceTrigger=PropertyChanged}" />
</DockPanel>
<Separator />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

我想在 ListeBoxItem 时引发事件被选中。

正如你所看到的,我已经尝试过 ListBoxItem.Selected="ListBoxItem_Selected"但它不起作用。

你有想法吗 ?

坦克提前!

最佳答案

您不会调用处理程序,因为 Selected 事件已由 ListBox 处理。您应该处理 SelectionChanged ListBox 中的事件:

<ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="ListBox_SelectionChanged">

或者,您可以使用 ItemContainerStyle 将处理程序附加到每个 ListBoxItem:
<ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="Selected" Handler="ListBoxItem_Selected"/>
</Style>
</ListBox.ItemContainerStyle>

关于wpf - 使用 ItemTemplate 在 ListBoxItems 构建上添加事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3599249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com