【问题标题】:DataTemplated ListBox items not responsive to mouse clicksDataTemplated ListBox 项目对鼠标单击没有响应
【发布时间】:2010-02-24 00:48:32
【问题描述】:

我正在尝试创建一个 ListBox,其中项目的文本绑定到其 ItemSource 中对象的 regionName 属性。这是按原样显示的,但是单击绑定创建的文本不会将 ListBoxItem 的状态更改为“已选择”(尽管在文本选择项目后单击空格。)当我使用 ListBox 时,ListBox 的行为符合预期DisplayMemberBinding 属性或当我将 Content 设置为 DataTemplate 中的随机静态文本时(单击文本选择 ListBoxItem)。但是,我想使用转换器来设置单个项目的文本颜色(不同的项目将是不同的颜色),所以我似乎必须使用 DataTemplate,它不能与 DisplayMemberBinding 属性一起使用.有没有其他人遇到过这个问题?

列表框的 XAML:

            <ListBox x:Name="x_UpdateAreaListBox" Margin="0,0,0,10" SelectionChanged="x_UpdateAreaListBox_SelectionChanged" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ListBoxItem Content="{Binding Path=regionName}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

【问题讨论】:

    标签: .net wpf xaml


    【解决方案1】:

    将 Datatemplate 中的 ListBoxItem 替换为 TextBlock 或 ContentControl。 ListBox 本身生成了 ListBox 项,因此数据模板中不必要的 ListBoxItem 会占用鼠标事件,这就是您看到奇怪行为的原因

    【讨论】:

      【解决方案2】:

      您需要使用 Label 或 TextBlock 而不是 ListBoxItem。 ListBox 会自动为您的 DataTemplated 数据生成容器。

      有关物品容器生成的更多信息,请参阅here

      【讨论】:

        猜你喜欢
        • 2010-11-19
        • 2010-10-03
        • 1970-01-01
        • 2022-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        相关资源
        最近更新 更多