猿问

WPF 在列表框中启用突出显示、复制和粘贴

我有一个要显示在菜单上的字符串列表。我使用了一个列表框,它的工作原理是它不会让我突出显示或复制/粘贴。


这是我的 XAML


<Grid>

    <Grid.ColumnDefinitions>

        <ColumnDefinition Width="500"/>

        <ColumnDefinition Width="500"/>

    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>

        <RowDefinition Height="450"/>

        <RowDefinition Height="318"/>

    </Grid.RowDefinitions>




    <ListBox Grid.Row="1" Grid.Column="1" x:Name="uiOCRData" />




</Grid>

这是我在 C# 中所拥有的


List<string> lines = new List<string>();

uiOCRData.ItemsSource = lines;

谢谢您的帮助!


月关宝盒
浏览 335回答 1
1回答

catspeake

您必须使用 aListBox.ItemTemplate以便您可以在ListBox.由于您希望能够选择文本等,最好的选择是使用TextBox.<ListBox Grid.Row="0" Name="uiOCRData">&nbsp; &nbsp; <ListBox.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBox Text="{Binding Path=.}"/>&nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; </ListBox.ItemTemplate></ListBox>编辑假设您想绑定到一些类对象的列表而不是简单的字符串列表。假设你的类看起来像这样:public class Data{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Name { get; set; }}然后你可以Properties像这样绑定到类中的任何一个:<ListBox Grid.Row="0" Name="uiOCRData">&nbsp; &nbsp; <ListBox.ItemTemplate>&nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBox Width="100" Text="{Binding Name}"/>&nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; </ListBox.ItemTemplate></ListBox>
随时随地看视频慕课网APP
我要回答