【问题标题】:Listbox selection change not firing on click on child in list box item列表框选择更改不会在单击列表框项中的子项时触发
【发布时间】:2015-03-07 13:25:10
【问题描述】:

我有一个 wpf 用户控件,其中包含一个绑定到我的视图模型的列表框。每个列表框项目由三个文本块组成。我制作 UI 的方式是当用户点击我的视图模型中的第一个 TextBlock(Title) 函数时会被调用。

唯一标识与现在单击的文本块对应的 ListBox 项。我在视图模型中添加了另一个属性,该属性绑定到列表框中的 SelectionChanged 事件。因此,每当我的 TextBlock 绑定命令被执行时,我将使用我的 SelectionChanged 属性来查找哪个列表框项并使用它的数据上下文。

但我现在面临的问题是当用户点击第一个文本块时,selectionchanged 事件没有触发。当用户在第一个文本块之外单击时,只会触发 Selection Changed 事件。这使我的视图模型处理错误的列表框项目。

以下是 XAML sn-p。

                <ListBox ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled"  ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedItem="{Binding SelectedNotificationItem}" ItemsSource="{Binding MyArray}" BorderThickness="0" Margin="0, 0, 0, 0" ItemContainerStyle="{StaticResource HoverBackgroundStyle}"  Name="NotificationListBox" >
                 <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Border BorderThickness="4,0,0,0" BorderBrush="{Binding ColorThing, Converter={StaticResource SeverityToColorConverter} }" Margin="0, 0, 0, 0">
                                <StackPanel Margin="8, 0" Orientation="Vertical">
                                    <TextBlock   Style="{StaticResource HoverUnderlineStyle}" FontWeight="Bold" TextTrimming="CharacterEllipsis" Name="Title" Text="{Binding Title}" TextWrapping="WrapWithOverflow" Margin="0,4,0,0" >
                                        <TextBlock.InputBindings>
                                            <MouseBinding MouseAction="LeftClick" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ClickTitleCommand}"></MouseBinding>
                                        </TextBlock.InputBindings>
                                    </TextBlock>

                                    <TextBlock  FontWeight="Normal" Name="Desc" Text="{Binding Description}" TextWrapping="WrapWithOverflow">

                                    </TextBlock>
                                    <TextBlock  FontWeight="Normal" Foreground="Gray" Name="Date" Text="{Binding CreationTime, StringFormat={}{0:ddd MM.dd.yyyy} }" Margin="0,4,0,4">
                                    </TextBlock>

                                </StackPanel>
                            </Border>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

【问题讨论】:

  • 您实际上是在尝试查找ListBox item(即UI 组件),还是对您的ItemsSource 中的哪个对象感兴趣?如果是后者,为什么不直接使用现有的SelectedItem 绑定来检测更改?
  • 但要回答你的问题,我怀疑SelectionChanged 没有触发,因为选择实际上并没有改变:第一个条目已经被选中,无论是自动还是因为你的SelectedItem绑定。
  • 我只对我的 ItemSource 中的对象感兴趣。问题是如果我们单击具有鼠标绑定的第一个文本块,则 selectedItem 不会触发。所以我找不到它。
  • 在这种情况下,我怀疑您的 MouseAction 输入绑定正在吞噬 SelectionChanged:您是否注意到当您单击文本框时,该行的选择不会改变(即颜色通常会改变与单击一行相关联不会发生)?在这种情况下,另一种方法是向此 ClickTitleCommand 添加一个 CommandParameter,该参数传入该行的项目 (CommandParameter="{Binding }"),然后您应该知道单击了哪个项目,并且还可以在如果你愿意,那一点。
  • 是的,谢谢我这样做了。添加的参数:CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" 和工作

标签: c# wpf xaml listbox listboxitem


【解决方案1】:

您可以尝试将您的 XAML 更改为 SelectedItem="{Binding SelectedNotificationItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

【讨论】:

  • 请考虑使用 MultiBinding,而不是在一个堆栈面板中使用多个文本块。请参阅以下链接:stackoverflow.com/questions/541896/…
  • 我期待动态更改第二个文本块中的某些属性。所有文本块的行为都应该不同。如果多绑定这可能吗?
  • 嗨 Ted,如果您在谈论 StringFormat,那么是的,这是可能的。您可以在需要时为整个多重绑定或单个绑定使用 StringFormat。
  • 不可以动态改变 textblock2 和 textblock3 的内容或 textblocks 的大小。谢谢
【解决方案2】:

将 ListBox 更改为 ItemsControl 并将方向更改为 horizo​​ntal

【讨论】:

  • 它将如何修复事件的触发?
  • 我认为这个答案没有帮助,也没有真正解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 1970-01-01
  • 2017-07-21
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多