如何使用IsMouseOver触发器将网格设置为focues?

时间:2014-03-07 15:58:55

标签: wpf

当我用鼠标悬停在xaml文件的基础上时,我正试图将焦点带到一个Grid元素。我有以下代码,目前没有设置焦点。

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

前两个触发器就是为了明确网格已经关注并且如果我在网格上选择了标记。第三个触发器是我试图设置焦点但它无法正常工作的地方。

3 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

如果我理解正确,我想你会想要这样的东西:

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

答案 2 :(得分:0)

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

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