PlTaylor
PlTaylor

Reputation: 7545

How do I set a grid as focues using a IsMouseOver trigger?

I am trying to bring focus to a Grid element that is the base of an xaml file when I hover over it with the mouse. I have the following code and it is not currently setting the focus.

<Grid x:Class="MyApp.MyClass"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         d:DataContext="{Binding Source={d:DesignInstance Type=trackPlot:MyViewModel}}"
         Focusable="True" 
         Name="MainGrid">
    <Grid.Style>
        <Style TargetType="{x:Type Grid}">
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="False">
                    <Setter Property="Background" Value="Transparent"/>
                </Trigger>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Green"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=MainGrid}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Style>
    ....Grid Contents here....
</Grid>

The first two triggers are just there to make it obvious that the Grid has been focused on and work if I tab onto the grid. The third trigger is where I am trying to set the focus and it is not functioning.

Upvotes: 1

Views: 316

Answers (3)

neptunao
neptunao

Reputation: 607

In your case I suggest you to use RelativeSource.Self in FocusedElement binding :

<Trigger Property="IsMouseOver" Value="True">
    <Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
</Trigger>

I think the reason is that binding expression searches element by element name without check target element name itself.

I can also provide my full example if its needed.

Upvotes: 1

PlTaylor
PlTaylor

Reputation: 7545

The answer to this question is to add an additional Grid layer and trigger on that. I have no idea why you can't use the base element of the file, but it doesn't seem to work.

<Grid x:Class="MyApp.MyClass"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"
     d:DataContext="{Binding Source={d:DesignInstance Type=trackPlot:MyViewModel}}">
    <Grid Focusable="True" Name="MainGrid">
        <Grid.Style>
            <Style TargetType="{x:Type Grid}">
                <Style.Triggers>
                    <Trigger Property="IsFocused" Value="False">
                        <Setter Property="Background" Value="Transparent"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Background" Value="Green"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=MainGrid}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
        ....Grid Contents here....
    </Grid>
</Grid>

Upvotes: 0

TMan
TMan

Reputation: 4122

If I understand correctly, I think you would want something like:

   <Trigger Property="IsMouseOver" Value="True">
     <Setter Property="IsFocused" Value="True" />
   </Trigger>

Upvotes: 0

Related Questions