是否可以在 WPF ListBox 控件中用鼠标选择单个单词?如果是,我该怎么做?
欢迎所有提示:)
如果ItemTemplate
为您的 定义 an ListBox
,则可以使用 aTextBox
来显示每个项目(假设您的项目是纯string
s):
<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>