Search code examples
wpfxamlwpf-style

How can I set ScrollContentPresenter a backgroundcolor?


How can I give ScrollContentPresenter a backgroundcolor? is the same question, but I set the Background on the Parent of the ScrollContentPresenter. It is the Grid and it does not work for me. I can not change the background.

<Style x:Key="ScrollViewerStyle" TargetType="{x:Type ScrollViewer}">
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Border x:Name="border" BorderBrush="{Binding L1, Source={x:Static color:DesignBrushCollection.DesignColors}}" BorderThickness="1">
                            <Grid x:Name="Grid" Background="Transparent">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="auto"/>
                                </Grid.ColumnDefinitions>
                                <Grid Background="Red">
                                <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="0" />
                                </Grid>
                                <ScrollBar x:Name="PART_VerticalScrollBar" AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0"  Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}" 
                                           Style="{DynamicResource ScrollBarStyle}"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger  Property="IsMouseOver" Value="true">
                                <Setter Property="BorderBrush"  TargetName="border"
                                        Value="{Binding AC3, Source={x:Static color:DesignBrushCollection.DesignColors}}"/>
                            </Trigger>
                            <Trigger  Property="IsFocused" Value="true">
                                <Setter Property="BorderBrush"  TargetName="border"
                                        Value="{Binding AC3, Source={x:Static color:DesignBrushCollection.DesignColors}}"/>
                                <Setter Property="Background" TargetName="border"
                                        Value="{Binding BG1, Source={x:Static color:DesignBrushCollection.DesignColors}}"/>
                            </Trigger>
                            <Trigger  Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" TargetName="border" Value="0.5"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

</UserControl.Resources>
    <Grid Width="auto" Height="auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="15" />
            <RowDefinition Height="3" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions >
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <local:CommonLabel  RequiredMark="{Binding RelativeSource={RelativeSource FindAncestor,
                    AncestorType={x:Type local:CommonComboBox}},Path=RequiredMark}"  
                                FontSize="13" Padding="0" 
                                Content="{Binding RelativeSource={RelativeSource FindAncestor,
                    AncestorType={x:Type local:CommonComboBox}},Path=Content}"
                                Grid.Row="0" Height="16" Width="139" 
                                Margin="0,0,0,0"  VerticalAlignment="Top" HorizontalAlignment="Left" Grid.ColumnSpan="2" />
        <ScrollViewer  Style="{StaticResource ScrollViewerStyle}" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Grid.Row="2">
            <TextBox x:Name="TextField" TextWrapping="Wrap" BorderThickness="0" CaretBrush="{Binding AC3, Source={x:Static color:DesignBrushCollection.DesignColors}}"
                     Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:CommonTextField}},Path=Content,Mode=OneWay}" >
            </TextBox>
        </ScrollViewer>
    </Grid>
</UserControl>

Solution

  • TextBox.Background does override the ScrollViewer.Background. If you set TextBox.Background = "Transparent" or TextBox.Background = "{x:Null}" you will see ScrollViewer.Background.

    <TextBox x:Name="TextField" Background="Transparent" ... />
    

    Be aware, that ScrollViewer.IsFocused set to true if you click the border of ScrollViewer or set the focus in code behind, not if you input a text in the TextBox.