【问题标题】:WPF ListBox select word with mouseWPF ListBox用鼠标选择单词
【发布时间】:2013-11-09 17:14:17
【问题描述】:

是否可以在 WPF ListBox 控件中用鼠标选择单个单词?如果是,我该怎么做?

欢迎所有提示:)

【问题讨论】:

    标签: c# .net wpf listbox


    【解决方案1】:

    如果您为您的ListBox 定义ItemTemplate,您可以使用TextBox 来显示每个项目(假设您的项目是普通的strings):

    <ListBox ItemsSource="{Binding YourCollection}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding}" IsReadOnly="True" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    更新>>>

    我刚刚对其进行了测试,不得不进行一项更改以将Binding.Mode 属性设置为OneWay,它工作得很好。但是,我注意到 TextBox 会阻止每个项目被选中,所以添加了一个 Style 来处理这个问题,并对项目进行了一些样式设置:

    <ListBox ItemsSource="{Binding YourCollection}" Name="ListBox" HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding ., Mode=OneWay}" IsReadOnly="True">
                    <TextBox.Style>
                        <Style>
                            <Setter Property="TextBox.BorderThickness" Value="0" />
                        </Style>
                    </TextBox.Style>
                </TextBox>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style>
                <Style.Triggers>
                    <Trigger Property="ListBox.IsKeyboardFocusWithin" Value="True">
                        <Setter Property="ListBoxItem.IsSelected" Value="True" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
    

    【讨论】:

    • 我就是这么做的。但是当我用鼠标选择一个项目时,我会选择 ListBox 中的整个项目行而不是一个单词。
    • 我会试一试的!谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多