【问题标题】:How to put editable text in static text paragraph in WPF如何在 WPF 中的静态文本段落中放置可编辑文本
【发布时间】:2015-09-04 01:03:39
【问题描述】:

我想要实现的目标在 Web 应用程序中有些简单,但我一直在 WPF 中努力做到这一点。

我想在 WPF 中加载一段文本,并用可编辑的文本框替换其中的一些特定单词。我该怎么做?

以正确、干净的方式实现这一目标的最佳策略是什么?

更新:

考虑以下文本。我想在 WPF 中显示它,而不是用粗体字放置一些文本框。

你认识一个有钱有名的人吗?他是否自信、受欢迎、 一直快乐——主流成功的缩影?或者, 另一方面,他是否感到压力大,对自己的生活选择有第二个想法,并且不确定他的生活的意义?

【问题讨论】:

  • 他的文字是什么重要吗?除了他问是否有一种“干净”的方式来做这件事之外,你显然可以在一个文本块上扔一堆文本框,但这并不令人满意。
  • Ryan Searle 他必须向我们展示……然后我们可以指导他编写更好的代码。既然我们不知道他的代码是什么,我们该如何帮助他?!我应该说这个问题没有最干净的代码。当有代码时,我们将代码与其他代码进行比较!
  • @Arashjo 这是一个没有任何格式的简单文本。我用文本样本更新了问题。关于代码,老实说我不知道​​如何编程。

标签: c# wpf textbox richtextbox


【解决方案1】:

WPF 和 XAML 与 HTML 不同,都是关于 数据

考虑和推理任何基于 XAML 的 UI 的最佳方式是考虑您需要显示和交互的数据

在这种情况下:

public class Word
{
    public string Value { get; set; }

    public bool IsEditable { get; set; }
}

将代表我们的每一个词。那么你只需要一个列表:

public class ViewModel
{
    public List<Word> Words { get; private set; }

    public ViewModel()
    {
        var editableWords = new[] { "on", "of" };

        var text = "Do you know someone rich and famous? Is he confident, popular, and joyful all of the time—the epitome of mainstream success? Or, on the other hand, is he stressed, having second thoughts about his life choices, and unsure about the meaning of his life?";

        this.Words =
            text.Split(' ')
                .Select(x => new Word
                {
                    Value = x,
                    IsEditable = editableWords.Contains(x.ToLower())
                })
                .ToList();
    }
}

请注意我如何将文本转换为 List&lt;Word&gt; 并在需要的位置设置 IsEditable

现在只需使用ItemsControl 来显示这些:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis"/>
    </Window.Resources>
    <ItemsControl ItemsSource="{Binding Words}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Margin="5,2,5,2">
                    <TextBlock Text="{Binding Value}"
                               VerticalAlignment="Center"/>
                    <TextBox Text="{Binding Value}"
                             Visibility="{Binding IsEditable, Converter={StaticResource BoolToVis}}"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

最后,将DataContext 设置为我们的 ViewModel 的一个实例:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new ViewModel();
    }
}

结果:

请注意,我根本没有在代码中“接触” UI,这只是简单、简单的属性和 DataBinding。

【讨论】:

  • 非常感谢 HighCore,非常优雅,但是您是否遗漏了示例中的某些部分代码?我的什么都没有显示。也许是某事的参考?
  • @Vahid 确保您设置了 Window 的 DataContext,就像我在后面的代码中所做的那样。
  • 谢谢 HighCore。非常尊重。
猜你喜欢
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多