简体   繁体   English

如何用我自己的图像设置TextBox ScrollBar的样式?

[英]How to style a TextBox ScrollBar with my own images?

I was wondering if it is possible to insert my own bitmaps for scrollbar styling. 我想知道是否可以为滚动条样式插入我自己的位图。 I would like to to use an image for up/down arrows, background and scroller. 我想将图像用于上/下箭头,背景和滚动条。 This is what I have to start with, but I just don't know how to access these mentioned properties: 这是我必须开始的事情,但我只是不知道如何访问这些提到的属性:

  <TextBox Name="SlideNotes" Foreground="White" FontSize="20" FontWeight="Bold" Background="Transparent" BorderThickness="0" TextWrapping="Wrap" AcceptsReturn="True" ScrollViewer.VerticalScrollBarVisibility="Visible"  Grid.Row="27" Grid.Column="11" Grid.ColumnSpan="46" Grid.RowSpan="5">
                <TextBox.Resources>
                    <Style TargetType="{x:Type ScrollBar}">
                        <Setter Property="Background" Value="Red"/>

                    </Style>
                </TextBox.Resources>

I would appreciate any help. 我将不胜感激任何帮助。

Looks like you're after a ControlTemplate . 看起来您正在使用ControlTemplate See here for a nice tutorial on how to style one. 有关如何设置样式的很好的教程,请参见此处

To avoid having to style the entire ScrollViewer , you can instead style only the RepeatButton element, this will make things much easier to work with. 为了避免为整个 ScrollViewer设置样式,可以改为仅对 RepeatButton元素进行样式设置,这将使操作变得更加容易。

<Style TargetType="RepeatButton">
    <Setter Property="Background" Value="Red"/>
    <Setter Property="Template">
        <Setter.Value>
            ...
        </Setter.Value>
    </Setter>
</Style>

You may need to define this style in the Window Resources, instead of the TextBox resources due to the scope. 由于范围的原因,您可能需要在Window资源中定义此样式,而不是在TextBox资源中定义。

See the documentation for RepeatButton styles and templates. 请参阅有关RepeatButton样式和模板的文档

You can use ImageBrush to set an image to any property that uses Brushes ( Background , Foreground , BorderBrush , Fill , etc.). 您可以使用ImageBrush将图像设置为使用ImageBrush任何属性( BackgroundForegroundBorderBrushFill等)。

In your case, you'd use it like this: 在您的情况下,可以这样使用它:

<TextBox Name="SlideNotes" Foreground="White" FontSize="20" FontWeight="Bold" Background="Transparent" BorderThickness="0" TextWrapping="Wrap" AcceptsReturn="True" ScrollViewer.VerticalScrollBarVisibility="Visible"  Grid.Row="27" Grid.Column="11" Grid.ColumnSpan="46" Grid.RowSpan="5">
    <TextBox.Resources>
        <Style TargetType="{x:Type ScrollBar}">
            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="yourimage.jpg" Stretch="Fill" />
                </Setter.Value>
            </Setter>
        </Style>
    </TextBox.Resources>
</TextBox>

ImageBrush (and all other Brushes that inherit from TileBrush ) has a set of properties that control how the image is shown. ImageBrush (以及从TileBrush继承的所有其他TileBrush )具有一组控制如何显示图像的属性。 I've used Stretch="Fill" in my sample, which will make the image stretch to fill all the space available, but you could want it to behave differently. 我在示例中使用了Stretch="Fill" ,它将使图像拉伸以填充所有可用空间,但是您可能希望其行为有所不同。

For instance, this... 例如,这...

<ImageBrush ImageSource="yourimage.jpg" Stretch="None" TileMode="FlipXY" />

Will make your image repeat, with tiles alternatively flipped horizontally and/or vertically. 将使您的图像重复出现,并同时水平和/或垂直翻转图块。

Toy around with Stretch and TileMode (or even ViewPort , ViewPortUnit , Viewbox and ViewboxUnit , if you feel brave enough) to get the effect you want. 借助StretchTileMode (或者如果觉得足够勇敢,甚至可以使用ViewPortViewPortUnitViewboxViewboxUnit )来获得想要的效果。

EDIT - As with RepeatButton , the default ScrollBar template seems to pretty much ignore the Background property, most of the time, as well as other properties you could use for customization... This means you'll probably have to override the whole template to customize it to your liking. 编辑 -与RepeatButton ,默认情况下,默认的ScrollBar模板似乎几乎忽略了Background属性以及可用于自定义的其他属性...这意味着您可能必须覆盖整个模板以根据您的喜好自定义它。

Here's one ScrollBar template sample: ScrollBar Styles and Templates 这是一个ScrollBar模板示例: ScrollBar样式和模板

And here's another for the RepeatButton : RepeatButton Styles and Templates 这是RepeatButton的另一个: RepeatButton样式和模板

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

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