Search code examples
wpfdatagridscrollbar

WPF DataGrid Thumb opacity without assigning the whole scrollbar style


Is it possible to set WPF data grid scrollbar thumb opacity without changing the whole scrollbar template? I've been searching but what I've seen it was quite complex solutions based on setting up the whole scrollbar template, whereas I just need to change only one property. What I've tried so far is

            <DataGrid.Resources>
                <Style TargetType="Thumb">
                    <Setter Property="Opacity" Value="0.5"/>
                </Style>
            </DataGrid.Resources>

does not work, and

            <DataGrid.Resources>
                <Style TargetType="ScrollBar">
                    <Setter Property="Track.Thumb.Opacity" Value="0.5"/>
                </Style>
            </DataGrid.Resources>

gives "nested types are not supported error".

Or, may be it is possible from code-behind to write something like

myGrid.VerticalScrollBar.Track.Thumb.Opacity = 0.5;

(but the porblem is that grid has no such property..)

?


Solution

  • You can use the solution of CrimsonX to get the the ScrollBar at runtime by it's name PART_VerticalScrollBar:

    ScrollBar scrollBar = FindChild<ScrollBar>(myGrid, "PART_VerticalScrollBar");
    scrollBar.Track.Thumb.Opacity = 0.1;
    

    Note this only works if the Window is already rendered. This means it could be possible to set the opaticity in code behind after rendering. One way to do this is by overriding:

    protected override void OnContentRendered(EventArgs e)