gpt4 book ai didi

wpf - 如何支持回车键使焦点在 ListView 的文本栏中移动

转载 作者:行者123 更新时间:2023-12-02 20:27:18 25 4
gpt4 key购买 nike

我创建了以下 View

        <ListView.View>
<GridView>
<GridViewColumn Header="Tester"
DisplayMemberBinding="{Binding User}" />
<GridViewColumn Header="Executed On"
DisplayMemberBinding="{Binding ExecutionDate}" />
<GridViewColumn Header="Comment">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<TextBox Text="{Binding Comment}" Name="TxtComment"
MinWidth="100" VerticalContentAlignment="Top"
BorderThickness="0" PreviewKeyDown="TxtComment_PreviewKeyDown"/>
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>

现在我的想法是,在“评论”字段中输入内容并按“Enter”键后,“评论”字段的下一行将获得焦点。

我添加了事件“PreviewKeyDown”的以下代码,但似乎下整行不仅会在“注释”字段中获得焦点...

    Private Sub TxtComment_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
Dim focusRequest As TraversalRequest
Dim focusedElement As Object = sender

Select Case e.Key
Case Key.Enter
focusRequest = New TraversalRequest(FocusNavigationDirection.Down)
Case Else
' Do nothing
Return
End Select
' Do not further propagate event
e.Handled = True
'Move focus
DirectCast(focusedElement, UIElement).MoveFocus(focusRequest)
End Sub

希望有人能告诉我如何解决这个问题,^0^

最佳答案

将 SelectionChanged 事件添加到 ListView 以与 Enter 键盘事件链接,并使用 VisualTreeHelper 查找文本框。

这是 ListView XAML;与您的代码相同,只是我使用 ObservableCollection 在 ListView 中加载数据(未显示),并且添加了 SelectionChanged 事件:

<ListView x:Name="myView" ItemsSource="{Binding Path=MyData}"  
SelectionChanged="myView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Tester"
DisplayMemberBinding="{Binding User}" />
<GridViewColumn Header="Executed On"
DisplayMemberBinding="{Binding ExecutionDate}" />
<GridViewColumn Header="Comment">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<TextBox Text="{Binding Comment}" Name="TxtComment"
MinWidth="100" VerticalContentAlignment="Top"
BorderThickness="0"
PreviewKeyDown="TxtComment_PreviewKeyDown"/>
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

以下是在代码绑定(bind)中使用 VisualtreeHelper 的事件处理程序和辅助函数:

private void TxtComment_PreviewKeyDown(object sender, KeyEventArgs e)
{
TraversalRequest focusRequest = null;
TextBox focusedElement = sender as TextBox;

switch (e.Key)
{
case Key.Enter:
focusRequest = new TraversalRequest(FocusNavigationDirection.Down);
if ( focusedElement != null )
{
//Move focus
bool moved = focusedElement.MoveFocus(focusRequest);
if (moved)
{
e.Handled = true;
}
}
break;
default:
break;
}
}

// find the TextBox here
private void myView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView lv = sender as ListView;
if ( lv != null)
{
ItemContainerGenerator generator = lv.ItemContainerGenerator;
ListViewItem selectedItem = (ListViewItem) generator.ContainerFromIndex(lv.SelectedIndex);
TextBox tbFind = GetDescendantByType(selectedItem, typeof (TextBox), "TxtComment") as TextBox;
if (tbFind != null)
{
FocusHelper.Focus(tbFind);
}
}
}

public static Visual GetDescendantByType(Visual element, Type type, string name)
{
if (element == null) return null;
if (element.GetType() == type)
{
FrameworkElement fe = element as FrameworkElement;
if (fe != null)
{
if (fe.Name == name)
{
return fe;
}
}
}
Visual foundElement = null;
if (element is FrameworkElement)
(element as FrameworkElement).ApplyTemplate();
for (int i = 0;
i < VisualTreeHelper.GetChildrenCount(element);
i++)
{
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = GetDescendantByType(visual, type, name);
if (foundElement != null)
break;
}
return foundElement;
}

还有一个帮助程序可以将焦点设置在文本框上:

public static class FocusHelper
{
public static void Focus(UIElement element)
{
element.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(delegate()
{
element.Focus();
}));
}
}

关于wpf - 如何支持回车键使焦点在 ListView 的文本栏中移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3744258/

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