How do I change the background of my Grid layout when one of it’s children gets keyboard focus?

Platform: WPF| Category: Input and Commands.

You can change the background of the grid layout when one of its children gets keyboard focus by setting the IsKeyboardFocusWithin property. This can be done with the following code snippet,

[XAML]
<Window.Resources>
        <Style x:Key = "GridTriggerStyle" TargetType = "Grid">
            <Style.Triggers>
                <Trigger Property = "IsKeyboardFocusWithin" Value = "True">
                    <Setter Property = "Background" Value = "Green" />
                </Trigger>
                <Trigger Property = "IsKeyboardFocusWithin" Value = "False">
                    <Setter Property = "Background" Value = "AliceBlue" />
                </Trigger>
            </Style.Triggers>
       </Style>
    </Window.Resources>
     
    <Grid Name="testGrid" Style="{StaticResource GridTriggerStyle}">
        <Button Height="30" Width="100" Content="SampleButton1"/>
    </Grid>
       

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.