简体   繁体   English

如何使用WPF隐藏RichTextBox或TextBox中的部分文本?

[英]How to hide part of text in a richtextbox or textbox using WPF?

i would like to ask for aa way in wpf of hiding and unhiding some specific lines in a richtextbox or textbox using C# at Runtime and at the same time leave the rest of the lines visible. 我想问一种在运行时使用C#在Richtextbox或textbox中隐藏和取消隐藏某些特定行的wpf方式,同时让其余行可见。 I would also like not to be a visible space between the visible and unvisible lines. 我也不想在可见线和不可见线之间成为可见空间。 i have an idea of selecting the lines and then change font size to 0.01, but it isnt so much elegand. 我有一个选择线的想法,然后将字体大小更改为0.01,但这不是很多建议。 thanks in advance. 提前致谢。

By default, you can only hide and show certain text in a WPF TextBox or RichTextBox by changing the value of the Text or RichText properties respectively. 默认情况下,只能分别通过更改Text或RichText属性的值来隐藏和显示WPF TextBox或RichTextBox中的某些文本。 For example if you have the text "Mary had a little lamb who's fleece was white as snow" and wanted to hide the text "who's fleece was white as snow" conditonaly you would have to parse the text and remove or add "who's fleece was white as snow" in the correct location in the Text or RichText using code. 例如,如果您有文本“玛丽有一只小羊羔,其羊毛像雪一样洁白”,并且想要隐藏文本“谁的羊毛像雪一样洁白”,您将不得不解析文本并删除或添加“谁的羊毛像雪一样”。使用代码在“文本”或“ RichText”中的正确位置显示“白色如雪”。

It sounds like you are just trying to modify the layout depending on certain conditions though. 听起来您只是在尝试根据某些条件修改布局。 In that case, it is better to split the lines up into separate TextBoxes and set the visibility of those textboxes to Collapsed when certain conditions exist and set them to Visible otherwise. 在这种情况下,最好将行拆分为单独的文本框,并在存在某些条件时将这些文本框的可见性设置为“折叠”,否则将其设置为“可见”。

If you are using TextBox you will want an outer Border which will help you match the style of TextBox, then you can set the BorderBrush and Background of the textboxes you are hiding to Transparent so it just looks like a single TextBox. 如果您使用的是TextBox,则需要一个外部Border来帮助您匹配TextBox的样式,然后您可以将要隐藏的文本框的BorderBrush和Background设置为Transparent,这样看起来就像一个TextBox。

<Border
    BorderThickness="1"
    BorderBrush="Black"
    Background="White">
    <StackPanel>
        <TextBox
            Text="Mary had a little lamb "
            BorderBrush="Transparent"
            Background="Transparent" />

        <!-- Set the Visisibility Property of this TextBox in code to show or hide it -->
        <TextBox
            Name="ConditionalTextBoxLine"
            Visibility="Visible"
            Text="Who's fleece was white as snow"
            BorderBrush="Transparent"
            Background="Transparent" />
    </StackPanel>
</Border>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM