简体   繁体   English

RichTextBox FocusVisualStyle无法正常工作

[英]RichTextBox FocusVisualStyle not working

I have a RichTextBox that has some content. 我有一个RichTextBox,其中包含一些内容。 I want to hide the scrollbar and remove the border when the user moves the mouse over it. 我想隐藏滚动条并在用户将鼠标移到其上方时删除边框。 I have added FocusVisualStyle="{x:Null}" which I read in this link: Remove focus rectangle on a UserControl 我添加了在此链接中阅读的FocusVisualStyle =“ {x:Null}”: 删除UserControl上的焦点矩形

See below: 见下文:

 <RichTextBox HorizontalAlignment="Left" Height="105" Margin="48,42,0,0" VerticalAlignment="Top" Width="614" Background="Transparent" IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Hidden"  BorderBrush="Transparent" ScrollViewer.CanContentScroll="False"  FocusVisualStyle="{x:Null}">
      </FlowDocument>
         blah blah 
      </FlowDocument>
 </RichTextBox>

But wheneever the user moves the mouse over it I still see a white border appear and I can scroll up and down. 但是无论何时用户将鼠标移到它上面,我仍然会看到一个白色边框,并且可以上下滚动。

Why is this?? 为什么是这样??

That's not FocusVisual you are seeing instead its MouseOver effect which is applied by default for RichTextBox. 那不是FocusVisual ,而是您看到的默认情况下,RichTextBox应用了它的MouseOver effect

You need to override default template to remove that effect which is applied via trigger ( in case UIElement.IsMouseOver value is true ). 您需要重写默认模板以删除通过触发器应用的效果如果UIElement.IsMouseOver值为true )。

Create a style and provide your own template ( In case you want it to be applied for all RichTextBoxes in your app put this in app resources or window resources wherever it fits in your code. Otherwise you can declare it inline just for your RichTextbox): 创建样式并提供自己的模板(如果您希望将其应用于应用程序中的所有RichTextBoxes,请将其放入应用程序资源或窗口资​​源中适合代码的位置;否则,可以仅针对RichTextbox对其进行内联声明):

<Style TargetType="RichTextBox">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="TextBoxBase">
            <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
                    BorderBrush="{TemplateBinding Border.BorderBrush}"
                    Background="{TemplateBinding Panel.Background}"
                    Name="border"
                    SnapsToDevicePixels="True">
               <ScrollViewer HorizontalScrollBarVisibility="Hidden"
                             VerticalScrollBarVisibility="Hidden"
                             Name="PART_ContentHost"
                             Focusable="False" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="UIElement.IsEnabled" Value="False">
                    <Setter Property="UIElement.Opacity" TargetName="border" 
                            Value="0.56"/>
                </Trigger>
            <!--<Trigger Property="UIElement.IsMouseOver" Value="True">
                    <Setter Property="Border.BorderBrush" TargetName="border">
                       <Setter.Value>
                          <SolidColorBrush>#FF7EB4EA</SolidColorBrush>
                       </Setter.Value>
                    </Setter>
                </Trigger>-->
                <Trigger Property="UIElement.IsKeyboardFocused" Value="True">
                   <Setter Property="Border.BorderBrush" TargetName="border">
                      <Setter.Value>
                         <SolidColorBrush>#FF569DE5</SolidColorBrush>
                      </Setter.Value>
                   </Setter>
                 </Trigger>
              </ControlTemplate.Triggers>
           </ControlTemplate>
        </Setter.Value>
     </Setter>
  </Style>

Notice the trigger i have commented in the above default template. 注意我在上面的默认模板中已评论的触发器。

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

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