WPF how can i get focus from ScrollViewer ScrollContentPresenter

Viewed 39

that ScrollViewer.IsFocused set to true only if i click the border of ScrollViewer , not input a text in the TextBox or click the inside content,how can i get the focus to 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="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <DockPanel>
                            <Border x:Name="border" BorderBrush="{Binding L1, Source={x:Static color:DesignBrushCollection.DesignColors}}" BorderThickness="1">
                                <Grid x:Name="Grid">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid x:Name="text"   Background="{Binding BG3, Source={x:Static color:DesignBrushCollection.DesignColors}}">
                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
                                                            CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="0" Margin="{TemplateBinding Padding}" />
                                    </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>
                        </DockPanel>
                        <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="text"
                                        Value="{Binding BG1, Source={x:Static color:DesignBrushCollection.DesignColors}}"/>
                            </Trigger>
                            <Trigger  Property="IsEnabled" Value="False">
                                <Setter Property="Opacity"  Value="0.5"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                        
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
1 Answers

Instead of IsFocused property you should use property IsKeyboardFocusWithin in the Trigger, this will apply appropriate Setters, if any child control of ScrollViewer has a keyboard focus.

<Trigger  Property="IsKeyboardFocusWithin" Value="true">
Related