Wpf 如何使用IsMouseOver触发器将栅格设置为焦点?

Wpf 如何使用IsMouseOver触发器将栅格设置为焦点?,wpf,Wpf,当我用鼠标悬停在作为xaml文件基础的网格元素上时,我试图将焦点移到该网格元素上。我有下面的代码,它目前没有设置焦点 <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="

当我用鼠标悬停在作为xaml文件基础的网格元素上时,我试图将焦点移到该网格元素上。我有下面的代码,它目前没有设置焦点

<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>

…此处的网格内容。。。。

前两个触发器的作用是,如果我在网格上单击tab键,那么网格已经被关注并起作用。第三个触发器是我试图设置焦距的地方,但它不起作用。

如果我理解正确,我想您可能需要类似以下内容:

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

这个问题的答案是添加一个额外的网格层并触发它。我不知道为什么不能使用文件的基本元素,但它似乎不起作用

<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>

…此处的网格内容。。。。

在您的情况下,我建议您在FocusedElement绑定中使用RelativeSource.Self:

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

我认为原因是绑定表达式逐元素名称搜索元素,而不检查目标元素名称本身


如果需要,我还可以提供完整的示例。

它在空项目中运行良好。你能提供更多的去验证的xaml吗?这在一个空的项目中工作……现在是时候弄清楚区别了。很抱歉,没有先尝试一下,打扰了大家。你确定你的网格内容没有碍事吗?我已经解决了这个问题……我将用附加信息和答案更新我的问题。如果有人能解释为什么它是这样工作的,我会给他们答案。IsFocused不是一个可写的属性。这是有效的,并且有摆脱元素的额外好处。这让我想起了如何在没有名字的情况下进行自我参照。谢谢,我很感激。