【问题标题】:WPF Enable Highlight, Copy, and Paste inside a ListboxWPF 在列表框中启用突出显示、复制和粘贴
【发布时间】:2019-01-11 14:07:55
【问题描述】:

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

这是我的 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;

感谢您的帮助!

【问题讨论】:

  • 使用文本框作为 Listbox.ItemTemplate

标签: c# wpf xaml listbox


【解决方案1】:

您必须使用ListBox.ItemTemplate,以便您可以在ListBox 中包含一个控件。

由于您希望能够选择文本等,因此最好的选择是使用TextBox

<ListBox Grid.Row="0" Name="uiOCRData">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=.}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

编辑

假设您想绑定到一些类对象的列表,而不是简单的字符串列表。假设您的班级如下所示:

public class Data
{
    public int Id { get; set; }
    public string Name { get; set; }
}

然后你可以像这样绑定到任何一个选择的类Properties

<ListBox Grid.Row="0" Name="uiOCRData">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Width="100" Text="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

【讨论】:

  • 当我使用这个时,我的 ui 中什么也没有出现。我做了
  • 如果您使用List&lt;string&gt; 作为您的ItemsSource,那么绑定必须与我显示的完全相同:&lt;TextBox Text="{Binding Path=.}"/&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
相关资源
最近更新 更多