XAML 如何更改滚动条中滚动框或“拇指”的颜色?

dbf7pr2w  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(119)

如何将滚动条中的thump/elevator/scrollbox的颜色从灰色更改为红色?默认为灰色

<ScrollBar Width="10" Height="200" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Minimum="0" Background="Pink" ViewportSize="2"/>

fkaflof6

fkaflof61#

您需要为ScrollBar定义一个(完整的)自定义控件模板,例如here
或者,您可以在运行时以编程方式更改默认模板Thumb元素中生成的RectangleFill属性:

private void ScrollBar_Loaded(object sender, RoutedEventArgs e)
{
    ScrollBar scrollBar = (ScrollBar)sender;
    Thumb thumb = FindVisualChild<Thumb>(scrollBar);
    Rectangle rectangle = FindVisualChild<Rectangle>(thumb);
    rectangle.Fill = Brushes.Red;
}

XAML:

<ScrollBar Width="10" Height="200" AutomationProperties.AutomationId="VerticalScrollBar" 
           Cursor="Arrow" Minimum="0" Background="Pink" ViewportSize="2"
           Loaded="ScrollBar_Loaded" />

由于默认模板是如何定义的,恐怕您不能通过简单地设置ScrollBar元素的某些属性或使用隐式Style来实现这一点。

相关问题