Có lẽ tôi đang thiếu một cái gì đó nhưng tôi không thể tìm thấy bất kỳ phương pháp đơn giản để làm điều này, đây sẽ là một phác thảo về những gì bạn có thể làm:
<ListView.InputBindings>
<KeyBinding Key="Tab" Command="{Binding GoToNextItem}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListView}}" />
<KeyBinding Modifiers="Shift" Key="Tab" Command="{Binding GoToPreviousItem}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListView}}" />
</ListView.InputBindings>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="Selected" Handler="ItemSelected" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="number" />
<GridViewColumn Header="Selector">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Name="_tb" Text="{Binding SelectorName}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
Mọi thứ i đã làm ở đây:
- Hành vi tab ghi đè để kích hoạt lệnh để chọn một mục khác
- Thêm xử lý sự kiện cho sự kiện được lựa chọn để tập trung TextBox
- Tên TextBox để nó có thể được tìm thấy và tập trung
Code:
private readonly ICommand _GoToNextItem = new Command((p) =>
{
var lv = p as ListView;
if (lv.SelectedIndex == -1 || lv.SelectedIndex == lv.Items.Count - 1)
{
lv.SelectedIndex = 0;
}
else
{
lv.SelectedIndex++;
}
});
public ICommand GoToNextItem { get { return _GoToNextItem; } }
private readonly ICommand _GoToPreviousItem = new Command((p) =>
{
var lv = p as ListView;
if (lv.SelectedIndex <= 0)
{
lv.SelectedIndex = lv.Items.Count - 1;
}
else
{
lv.SelectedIndex--;
}
});
public ICommand GoToPreviousItem { get { return _GoToPreviousItem; } }
private void ItemSelected(object sender, RoutedEventArgs e)
{
var item = sender as ListBoxItem;
(FindNamedChild(item, "_tb") as TextBox).Focus();
}
public static object FindNamedChild(DependencyObject container, string name)
{
if (container is FrameworkElement)
{
if ((container as FrameworkElement).Name == name) return container;
}
var ccount = VisualTreeHelper.GetChildrenCount(container);
for (int i = 0; i < ccount; i++)
{
var child = VisualTreeHelper.GetChild(container, i);
var target = FindNamedChild(child, name);
if (target != null)
{
return target;
}
}
return null;
}
này rất sơ sài, sử dụng bất kỳ phần điều này có nguy cơ của riêng bạn. (Các hội tụ toàn cũng có thể đã được thực hiện cách khác nhau mà không mang lại sự lựa chọn thành này tôi nghĩ rằng)
(Lớp Command
chỉ là một việc thực hiện chung của ICommand
mà phải mất một lambda được thực hiện trong phương pháp của giao diện Execute
)
Nguồn
2011-08-11 03:47:26
Cảm ơn bạn H.B. tôi thực sự đã kết thúc không sử dụng nó vì nó đã gây phiền nhiễu khi u đã có rất nhiều hộp văn bản u sẽ không nhận được ra khỏi ListView mặc dù u muốn. Và tôi không có ý tưởng rằng SHIFT + TAB có thể chọn TreeviewItem một lần nữa và sau đó u có thể lặp với Mũi tên Lên và Xuống Và vẫn có Tùy chọn nhấn Tab để thoát khỏi TreeView sang Điều khiển tiếp theo. –
@Mohamed Cheri: Có, bị chặn thẻ không phải là rất thuận tiện. Tôi hy vọng điều này vẫn là của một số bạn. –