2016-07-03 50 views
1

我想更改RichTextBox的拇指/滚动条的颜色。c#wpf更改拇指/滚动条的颜色RichTextBox

我使用,但它并没有改变拇指

<RichTextBox x:Name="richTextBox" HorizontalAlignment="Left" Height="285" VerticalAlignment="Top" Width="880" VerticalScrollBarVisibility="Visible" IsReadOnly="True" Foreground="#FFA02626" Background="{x:Null}"> 
      <RichTextBox.Resources> 
       <Style TargetType="ScrollBar"> 
        <Setter Property="Foreground" Value="Red"/> 
       </Style> 
      </RichTextBox.Resources> 
</RichTextBox> 

回答

1

的颜色我没有找到任何简单的方法来做到这一点。但是你可以定义你的自定义ScrollBar风格。 这是我刚才写的例子。它看起来很丑,因为我要睡觉了。

<RichTextBox>    
      <RichTextBox.Resources> 
      <Style x:Key="ScrollThumb" TargetType="{x:Type Thumb}"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type Thumb}"> 
          <Rectangle x:Name="Rectangle1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="Transparent" SnapsToDevicePixels="True"/> 
          <ControlTemplate.Triggers> 
           <Trigger Property="Tag" Value="Horizontal"> 
            <Setter TargetName="Rectangle1" Property="Width" Value="Auto" /> 
            <Setter TargetName="Rectangle1" Property="Height" Value="7" /> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
      <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}"> 
       <Setter Property="Stylus.IsFlicksEnabled" Value="false" /> 
       <Setter Property="Foreground" Value="Red" /> 
       <Setter Property="Background" Value="DarkGray" /> 
       <Setter Property="Width" Value="10" /> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type ScrollBar}"> 
          <Grid x:Name="GridRoot" Width="19" Background="{TemplateBinding Background}"> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="0.00001*" /> 
           </Grid.RowDefinitions> 

           <Track x:Name="PART_Track" Grid.Row="0" IsDirectionReversed="true" Focusable="false"> 
            <Track.Thumb> 
             <Thumb x:Name="Thumb" Background="{TemplateBinding Foreground}" Style="{DynamicResource ScrollThumbs}" /> 
            </Track.Thumb> 
            <Track.IncreaseRepeatButton> 
             <RepeatButton x:Name="PageUp" Command="ScrollBar.PageDownCommand" Opacity="0" Focusable="false" /> 
            </Track.IncreaseRepeatButton> 
            <Track.DecreaseRepeatButton> 
             <RepeatButton x:Name="PageDown" Command="ScrollBar.PageUpCommand" Opacity="0" Focusable="false" /> 
            </Track.DecreaseRepeatButton> 
           </Track> 
          </Grid> 

          <ControlTemplate.Triggers> 
           <Trigger SourceName="Thumb" Property="IsMouseOver" Value="true"> 
            <Setter Value="{DynamicResource ButtonSelectBrush}" TargetName="Thumb" Property="Background" /> 
           </Trigger> 
           <Trigger SourceName="Thumb" Property="IsDragging" Value="true"> 
            <Setter Value="{DynamicResource DarkBrush}" TargetName="Thumb" Property="Background" /> 
           </Trigger> 

           <Trigger Property="IsEnabled" Value="false"> 
            <Setter TargetName="Thumb" Property="Visibility" Value="Collapsed" /> 
           </Trigger> 
           <Trigger Property="Orientation" Value="Horizontal"> 
            <Setter TargetName="GridRoot" Property="LayoutTransform"> 
             <Setter.Value> 
              <RotateTransform Angle="-90" /> 
             </Setter.Value> 
            </Setter> 
            <Setter TargetName="PART_Track" Property="LayoutTransform"> 
             <Setter.Value> 
              <RotateTransform Angle="-90" /> 
             </Setter.Value> 
            </Setter> 
            <Setter Property="Width" Value="Auto" /> 
            <Setter Property="Height" Value="12" /> 
            <Setter TargetName="Thumb" Property="Tag" Value="Horizontal" /> 
            <Setter TargetName="PageDown" Property="Command" Value="ScrollBar.PageLeftCommand" /> 
            <Setter TargetName="PageUp" Property="Command" Value="ScrollBar.PageRightCommand" /> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </RichTextBox.Resources> 
    </RichTextBox> 

可以提高它遵循MS指导这里:https://msdn.microsoft.com/en-us/library/ms742173(v=vs.110).aspx 或者在这里:https://www.google.com

+0

将它从滚动RichTextBox的工作??? – K4CZP3R

+0

为什么不尝试复制我的代码并粘贴到网格内的xaml代码 – tandat