0

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

欢迎所有提示:)

4

1 回答 1

1

如果ItemTemplate为您的 定义 an ListBox,则可以使用 aTextBox来显示每个项目(假设您的项目是纯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>
于 2013-11-09T17:21:50.880 回答