【发布时间】:2013-11-09 17:14:17
【问题描述】:
是否可以在 WPF ListBox 控件中用鼠标选择单个单词?如果是,我该怎么做?
欢迎所有提示:)
【问题讨论】:
是否可以在 WPF ListBox 控件中用鼠标选择单个单词?如果是,我该怎么做?
欢迎所有提示:)
【问题讨论】:
如果您为您的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>
【讨论】: