【发布时间】:2015-05-18 17:30:15
【问题描述】:
网上到处都是类似的问题,我也搜索了 SO(最接近的:[1]、[2]、[3])。
到目前为止,我无法相信这个问题是如此丑陋/不平凡。
我有一个 DataGridView。我有其他控件(在这种情况下:在 DataGridView 的列标题中)。我想支持一个快速的“跳转到网格”命令。此命令需要将键盘焦点设置为网格,以便用户可以使用箭头键在行之间导航。
在下面在家玩的简单测试用例。在数据网格中选择一个元素非常容易,但似乎没有办法让它成为键盘焦点。除了在代码隐藏操作中,即使那样,您似乎也必须跳过箍(请参阅 [2],杂耍让单元格容器设置键盘焦点,因为 .. 网格和行似乎并不适用于所有我知道)。
看起来很简单?
一些简单的模型:
public class Item
{
public int DayOfMonth { get; set; }
public string Weekday { get; set; }
}
匹配琐碎的视图模型(假设您有一个 RelayCommand 实现,请参阅 JumpToGrid for the ~meat~):
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private IList<Item> m_items;
public IList<Item> SampleItems
{
get { return m_items; }
set { SetField(ref m_items, value, () => SampleItems); }
}
private Item m_currentItem;
public Item CurrentItem
{
get { return m_currentItem; }
set { SetField(ref m_currentItem, value, () => CurrentItem); }
}
public ICommand JumpToGridCommand { get; private set; }
public MainViewModel()
{
JumpToGridCommand = new RelayCommand(p => JumpToGrid());
var items = new List<Item>();
var today = DateTime.Now;
for (int i = 1; i <= DateTime.DaysInMonth(today.Year, today.Month); i++ )
{
items.Add(new Item { DayOfMonth = i, Weekday = new DateTime(today.Year, today.Month, i).DayOfWeek.ToString() });
}
SampleItems = items;
}
private void JumpToGrid()
{
// I can change the selection just fine
CurrentItem = SampleItems[0];
// But the keyboard focus is broken, up/down doesn't work as expected
}
protected bool SetField<T>(ref T field, T value, Expression<Func<T>> selectorExpression)
{
if (selectorExpression == null) throw new ArgumentNullException("selectorExpression");
MemberExpression body = selectorExpression.Body as MemberExpression;
if (body == null) throw new ArgumentException("The body must be a member expression");
var fieldName = body.Member.Name;
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(fieldName);
return true;
}
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
简单视图(代码隐藏为空,加载事件中的“DataContext = new MainViewModel()”除外):
<Window x:Class="DataGridKeyboardFocus.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGridKeyboardFocus"
Loaded="Window_Loaded"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid
ColumnWidth="*"
AutoGenerateColumns="False"
Margin="1"
ItemsSource="{Binding SampleItems}"
SelectedItem="{Binding CurrentItem, Mode=TwoWay}"
SelectionUnit="FullRow"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding DayOfMonth}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="Some label" />
<TextBox>
<TextBox.InputBindings>
<KeyBinding Modifiers="Control" Key="Tab" Command="{Binding DataContext.JumpToGridCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" />
</TextBox.InputBindings>
</TextBox>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Weekday}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="Another label" />
<TextBox>
<TextBox.InputBindings>
<KeyBinding Modifiers="Control" Key="Tab" Command="{Binding DataContext.JumpToGridCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" />
</TextBox.InputBindings>
</TextBox>
</StackPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
【问题讨论】:
标签: .net wpf datagridview wpfdatagrid