【问题标题】:Show/Hide Left and Right Border in WPF RichTextBox codebehind?在 WPF RichTextBox 代码隐藏中显示/隐藏左右边框?
【发布时间】:2016-02-03 12:04:30
【问题描述】:

我想有时在代码后面隐藏/显示 WPF RichTextBox 左边框,有时也隐藏/显示右边框。

这可以实现吗?

我已经尝试过像 borderthickness ="3,3,0,3" 这样的 XAML。它隐藏了我的右侧,现在我需要显示右侧边框并隐藏我的右侧边框。

我该怎么做?

更新:

<Window x:Class="View.SingleLineTextMode"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SingleLineTextMode" Height="300" Width="300" WindowState="Maximized" WindowStyle="None" WindowStartupLocation="CenterScreen" Background="Black">

    <Window.Resources>
        <Style x:Key="MyStyle" BasedOn="{x:Null}" TargetType="{x:Type RichTextBox}">
            <Setter Property="BorderThickness" Value="3"/>
            <Setter Property="Padding" Value="0,5,0,0"/>

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RichTextBox}">
                        <Border x:Name="bg" BorderBrush="Yellow" BorderThickness="3,3,0,3" Background="{TemplateBinding Background}">
                            <ScrollViewer x:Name="PART_ContentHost"  IsDeferredScrollingEnabled="True" CanContentScroll="False" />
                        </Border>
                        <ControlTemplate.Triggers>

                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="Yellow"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="3,3,0,3"/>
                            </Trigger>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="Yellow"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="3,3,0,3"/>
                            </Trigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <!--<RichTextBox Name="txtAppendValue" Height="60" Width="1920" Style="{StaticResource MyStyle}" Margin="0,0,-10,0" FontSize="60" FontFamily="Arial"  IsReadOnly="True" Focusable="False" Cursor="Arrow" BorderThickness="3,3,3,3" IsUndoEnabled="False" UndoLimit="0" TextOptions.TextFormattingMode="Display" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.CacheLength="2,3" VirtualizingPanel.CacheLengthUnit="Page" TextBlock.LineHeight="100" Padding="0">
        </RichTextBox>-->
        <RichTextBox Name="txtAppendValue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="60" Width="1920" Style="{StaticResource MyStyle}" Margin="0,0,0,0"  FontSize="40" FontFamily="Arial"  IsReadOnly="True" Focusable="False" Cursor="Arrow"  IsUndoEnabled="False" UndoLimit="0" TextOptions.TextFormattingMode="Display" VirtualizingPanel.IsVirtualizing="True" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.CacheLength="2,3" VirtualizingPanel.CacheLengthUnit="Page" TextBlock.LineHeight="100" >
        </RichTextBox>
        <Label Name="lblStatusUpdate" ></Label>
    </Grid>
</Window>

以上是我的XAML。

txtAppendValue.BorderThickness=new Thickness("0,3,0,3");

但它不起作用。

问候 阿琼

【问题讨论】:

  • 问题是你使用的风格,我已经对我的答案进行了编辑。

标签: c# wpf border richtextbox


【解决方案1】:

你可以用这样的代码做同样的事情:

myRichTextBox.BorderThickness = new Thickness(3,3,0,3);

那么如果所有边框都需要具有相同的粗细,你可以只使用一个数字:

myRichTextBox.BorderThickness = new Thickness(3);

编辑:

由于使用的是样式,所以可以定义多种样式,然后在它们之间切换:

<Style x:Key="MyStyleNoBorders" BasedOn="{x:Null}" TargetType="{x:Type RichTextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RichTextBox}">
                <Border x:Name="bg" BorderBrush="Yellow" BorderThickness="0,3,0,3" Background="{TemplateBinding Background}">
                    <ScrollViewer x:Name="PART_ContentHost"  IsDeferredScrollingEnabled="True" CanContentScroll="False" />
                </Border>                       
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="MyStyleBothBorders" BasedOn="{x:Null}" TargetType="{x:Type RichTextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RichTextBox}">
                <Border x:Name="bg" BorderBrush="Yellow" BorderThickness="3" Background="{TemplateBinding Background}">
                    <ScrollViewer x:Name="PART_ContentHost"  IsDeferredScrollingEnabled="True" CanContentScroll="False" />
                </Border>                       
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

确保您的RichTextBox 不会太宽而无法适应屏幕,否则您将无法看到边框。然后在代码中只需使用调用来更改样式:

txtAppendValue.Style = FindResource("MyStyleNoBorders") as Style;

txtAppendValue.Style = FindResource("MyStyleBothBorders") as Style;

【讨论】:

  • @ Nemanja : No man.its not working 'txtAppendValue.BorderThickness = new Thickness(0, 3, 0, 3);'我的意思是不要隐藏我的左边框。
  • 我不确定我是否理解你...只需使用大于 0 的数字作为你希望看到的边框,在此处查看 Thickness 构造函数:Thickness Constructor
  • @ Nemanja :是的。当我们使用一些数字时,它会增加边框的厚度。但我需要隐藏那个边框。
  • 不将特定边框的数字设置为 0 会得到您需要的结果吗?或者您是否正在寻找一种不同的方式来隐藏边框?
  • 其实没用。最初我使用边距值,如 0,0,0,0.and borderthickness ="3,3,0,3" 它很好并隐藏右侧。之后我必须更改borderthickness ="0,3,0,3 " 用于两侧隐藏和 0,3,3,3 用于左侧隐藏。但它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多