KeyBoard focus wpf:从不同的 UserControls TreeView 按下 TAB 时,将 KeyBoard Focus 设置为 DataGrids 第一行
KeyBoard focus wpf : Set KeyBoard Focus to a DataGrids First row when Pressed TAB from a different UserControls TreeView
我有两个 UserControls
一个有一个 TreeView
另一个有一个 Button
和一个 DataGrid
.
我想要实现的是,当 TreeViewItem
上的 Tab
应该将 KeyBoard Focus 提供给第二个 UserControl 中的 DataGrid。
我查看了不同的帖子,但没有成功。在下面找到我的 XAML,
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<UC:ExplorerView DataContext="{Binding ExplorerViewModel}" Grid.Row="0"/>
<UCs:TableOfContentView DataContext="{Binding TableOfContentViewModel}" x:Name="TOCView" Grid.Row="1"/>
</Grid>
简化了XAML问题。
我试图通过添加事件 PreviewKeyDown
将焦点设置到第二个 UserControl
private void ExplorerView_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Tab)
{
Keyboard.Focus(TOCView);
}
}
但是正如我上面提到的那样,这将重点放在 USerControl 而不是 DataGrid 上。
试图Attach
一个Property
到DataGrid
。它按预期工作,但没有将焦点放在 first row
上。 this thread 给出输入。
已解决 :)
创建了上面线程中提到的 AttachedProperty
并修改了回调方法以将焦点设置到 DataGrids
第一行,如下所示,
private static object OnCoerceValue(DependencyObject d, object baseValue)
{
if (((DataGrid)d).HasItems)
{
DataGridRow row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
if(row !=null)
{
if ((bool)baseValue)
{
FocusManager.SetIsFocusScope(row, true);
FocusManager.SetFocusedElement(row, row);
}
else if (((UIElement)d).IsFocused)
Keyboard.ClearFocus();
}
}
return ((bool)baseValue);
}
请随时添加任何更好的解决方案。提前致谢。
创建了上面线程中提到的 AttachedProperty
并修改了回调方法以将焦点设置到 DataGrids
第一行,如下所示,
private static object OnCoerceValue(DependencyObject d, object baseValue)
{
if (((DataGrid)d).HasItems)
{
DataGridRow row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
if(row !=null)
{
if ((bool)baseValue)
{
FocusManager.SetIsFocusScope(row, true);
FocusManager.SetFocusedElement(row, row);
}
else if (((UIElement)d).IsFocused)
Keyboard.ClearFocus();
}
}
return ((bool)baseValue);
}
我有两个 UserControls
一个有一个 TreeView
另一个有一个 Button
和一个 DataGrid
.
我想要实现的是,当 TreeViewItem
上的 Tab
应该将 KeyBoard Focus 提供给第二个 UserControl 中的 DataGrid。
我查看了不同的帖子,但没有成功。在下面找到我的 XAML,
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<UC:ExplorerView DataContext="{Binding ExplorerViewModel}" Grid.Row="0"/>
<UCs:TableOfContentView DataContext="{Binding TableOfContentViewModel}" x:Name="TOCView" Grid.Row="1"/>
</Grid>
简化了XAML问题。
我试图通过添加事件 PreviewKeyDown
UserControl
private void ExplorerView_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Tab)
{
Keyboard.Focus(TOCView);
}
}
但是正如我上面提到的那样,这将重点放在 USerControl 而不是 DataGrid 上。
试图Attach
一个Property
到DataGrid
。它按预期工作,但没有将焦点放在 first row
上。 this thread 给出输入。
已解决 :)
创建了上面线程中提到的 AttachedProperty
并修改了回调方法以将焦点设置到 DataGrids
第一行,如下所示,
private static object OnCoerceValue(DependencyObject d, object baseValue)
{
if (((DataGrid)d).HasItems)
{
DataGridRow row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
if(row !=null)
{
if ((bool)baseValue)
{
FocusManager.SetIsFocusScope(row, true);
FocusManager.SetFocusedElement(row, row);
}
else if (((UIElement)d).IsFocused)
Keyboard.ClearFocus();
}
}
return ((bool)baseValue);
}
请随时添加任何更好的解决方案。提前致谢。
创建了上面线程中提到的 AttachedProperty
并修改了回调方法以将焦点设置到 DataGrids
第一行,如下所示,
private static object OnCoerceValue(DependencyObject d, object baseValue)
{
if (((DataGrid)d).HasItems)
{
DataGridRow row = ((DataGrid)d).ItemContainerGenerator.ContainerFromIndex(0) as DataGridRow;
if(row !=null)
{
if ((bool)baseValue)
{
FocusManager.SetIsFocusScope(row, true);
FocusManager.SetFocusedElement(row, row);
}
else if (((UIElement)d).IsFocused)
Keyboard.ClearFocus();
}
}
return ((bool)baseValue);
}