GridView LostFocus 和 GotFocus 事件

GridView LostFocus and GotFocus events

每次焦点从一个 GridViewItem 转移到另一个时,GridView GotFocusLostFocus 似乎都会触发。有什么方法可以知道 GridView 作为一个整体何时获得焦点和失去焦点?

MyGridView.GotFocus += MyGridView_GotFocus;
MyGridView.LostFocus += MyGridView_LostFocus;

MyGridView

中,每次焦点从一个 GridViewItem 移动到另一个时,上述事件 MyGridView_GotFocus 和 MyGridView_LostFocus 都会触发

Is there any way to know when a GridView as a whole received and lost focus?

没有直接的方法可以做到这一点。我们只能使用变通方法来模仿该行为。基本思路是,由于GotFocusLostFocus都是Routed Events,我们可以在页面根Grid的GotFocus中模仿GridView的GotFocusLostFocus ] 和 LostFocus 个事件。

这里是 XAML:

<Grid Name="rootGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel Margin="100,50">
        <StackPanel VerticalAlignment="Center" >
            <GridView Name="myGridView"></GridView>
    </StackPanel>
        <TextBlock Name="myTb"></TextBlock>
        <GridView Name="myGridView2" ></GridView>
    <TextBlock Name="output">This is the OutPut Text: </TextBlock>
        <Button Name="btnFocus" >Focus Button</Button>
    </StackPanel>
</Grid>

代码如下:

public sealed partial class MainPage : Page
{
    public List<String> List;
    public List<String> List2;
    private object PreviousFocusItem;
    public MainPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        List = new List<string> { "data1_GridView1", "data2_GridView1", "data3_GridView1" };
        List2 = new List<string> { "data1_GridView2", "data2_GridView2", "data3_GridView2" };
        myGridView.ItemsSource = List;
        myGridView2.ItemsSource = List2;
        rootGrid.GotFocus += Grid_GotFocus;
        rootGrid.LostFocus += Grid_LostFocus;
        base.OnNavigatedTo(e);
    }

    private void Grid_LostFocus(object sender, RoutedEventArgs e)
    {
        PreviousFocusItem = e.OriginalSource;
    }

    private void Grid_GotFocus(object sender, RoutedEventArgs e)
    {
        //get the previous focus Element and current focus Element.
        var previous = PreviousFocusItem as UIElement;
        var current = e.OriginalSource as UIElement;

        //got focus logic
        if ((!IsElementInsideGridView(myGridView, previous)) &&IsElementInsideGridView(myGridView,current))
        {
            //gridView got focus as a whole, put your codes here:
            output.Text += "Got Focus+1 \n";
        }

        //lost focus logic
        if ((!IsElementInsideGridView(myGridView, current)) &&(IsElementInsideGridView(myGridView,previous)) )
        {
            //gridView lost focus as a whole, put your codes here:
            output.Text += "Lost Focus+1 \n";
        }
    }

    private bool IsElementInsideGridView(GridView gridView,UIElement element)
    {
        Point topLeft = gridView.TransformToVisual(this).TransformPoint(new Point());
        Rect rectBounds = new Rect(topLeft.X, topLeft.Y, gridView.ActualWidth, gridView.ActualHeight);
        IEnumerable<UIElement> hits = VisualTreeHelper.FindElementsInHostCoordinates(rectBounds, element);
        if (hits == null || hits.Count() == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

就像您看到的,每次根网格获得焦点时。如果 PreviousFocusItem 不在 TargetGridView 中,而 CurrentFocusItemTargetGridView 中。这意味着 TargetGridView 获得了整体焦点。对于 LostFocus,逻辑是相似的。

这是我的完整项目:GridViewFocusSample