gpt4 book ai didi

c# - GridView LostFocus 和 GotFocus 事件

转载 作者:行者123 更新时间:2023-11-30 23:23:12 24 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

这是 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;
}
}
}

如您所见,每次根 Grid 获得焦点时。如果 PreviousFocusItem 不在 TargetGridView 中,而 CurrentFocusItemTargetGridView 中。这意味着 TargetGridView 作为一个整体获得了焦点。对于 LostFocus,逻辑是相似的。

这是我的完整项目:GridViewFocusSample .

关于c# - GridView LostFocus 和 GotFocus 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38472816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com