我有 3 个 XamDataGrids,每个都有不同的来源,我希望不同的网格一起行动,所以如果选择第一个网格并按下右箭头,应该取消选择第一个网格,应该聚焦第二个网格,第一个项目在第二格应该被选中。这工作正常,但是,按向左箭头键似乎不起作用。焦点实际上向右移动,没有选择任何项目。用于确定下一个网格的逻辑对于左键单击和右键单击是相同的,但由于某种原因,只有左箭头键会导致问题。
景色:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<dataPresenter:XamDataGrid Grid.Column="0" x:Name="Grid1" DataSource="{Binding Grid1Source}" PreviewKeyDown="Handle_PreviewKeyDown"/>
<dataPresenter:XamDataGrid Grid.Column="1" x:Name="Grid2" DataSource="{Binding Grid2Source}" PreviewKeyDown="Handle_PreviewKeyDown"/>
<dataPresenter:XamDataGrid Grid.Column="2" x:Name="Grid3" DataSource="{Binding Grid3Source}" PreviewKeyDown="Handle_PreviewKeyDown"/>
</Grid>
代码隐藏:
public TestView()
{
InitializeComponent();
viewmodel = // Get ViewModel
DataContext = viewmodel;
viewmodel.Initialize();
}
private void Handle_PreviewKeyDown(object sender, KeyEventArgs e)
{
var currentGrid = sender as XamDataGrid;
switch (e.Key)
{
case Key.Left:
case Key.Right:
var grid = GetNextGrid(currentGrid, e);
if (grid != null)
{
ChangeGrid(currentGrid, grid);
}
break;
}
}
private void ChangeGrid(XamDataGrid previousGrid, XamDataGrid nextGrid)
{
previousGrid.SelectedItems.Records.Clear();
previousGrid.ActiveRecord = null;
nextGrid.SelectedItems.Records.Clear();
nextGrid.ActiveRecord = null;
nextGrid.SelectedItems.Records.Add(nextGrid.Records[0]);
nextGrid.SelectedDataItem = nextGrid.Records[0];
nextGrid.Focus();
}
private XamDataGrid GetNextGrid(XamDataGrid currentGrid, KeyEventArgs e)
{
switch (currentGrid.Name)
{
case "Grid1":
if (e.Key == Key.Left)
{
return Grid3.Records.Count > 0 ? Grid3 : Grid2.Records.Count > 0 ? Grid2 : null;
}
else if (e.Key == Key.Right)
{
return Grid2.Records.Count > 0 ? Grid2 : Grid3.Records.Count > 0 ? Grid3 : null;
}
break;
case "Grid2":
if (e.Key == Key.Left)
{
return Grid1.Records.Count > 0 ? Grid1 : Grid3.Records.Count > 0 ? Grid3 : null;
}
else if (e.Key == Key.Right)
{
return Grid3.Records.Count > 0 ? Grid3 : Grid1.Records.Count > 0 ? Grid1 : null;
}
break;
case "Grid3":
if (e.Key == Key.Left)
{
return Grid2.Records.Count > 0 ? Grid2 : Grid1.Records.Count > 0 ? Grid1 : null;
}
else if (e.Key == Key.Right)
{
return Grid1.Records.Count > 0 ? Grid1 : Grid2.Records.Count > 0 ? Grid2 : null;
}
break;
}
return null;
}
视图模型:
public class TestViewModel : INotifyPropertyChanged
{
#region variables
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<string> grid1Source = new ObservableCollection<string>();
private ObservableCollection<string> grid2Source = new ObservableCollection<string>();
private ObservableCollection<string> grid3Source = new ObservableCollection<string>();
public ObservableCollection<string> Grid1Source
{
get => grid1Source;
set
{
grid1Source = value;
RaisePropertyChanged(nameof(Grid1Source));
}
}
public ObservableCollection<string> Grid2Source
{
get => grid2Source;
set
{
grid2Source = value;
RaisePropertyChanged(nameof(Grid2Source));
}
}
public ObservableCollection<string> Grid3Source
{
get => grid3Source;
set
{
grid3Source = value;
RaisePropertyChanged(nameof(Grid3Source));
}
}
#endregion
#region INotify
public void RaisePropertyChanged(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
# endregion
public TestViewModel()
{
Grid1Source = new ObservableCollection<string> { "Grid1 Test1", "Grid1 Test2", "Grid1 Test3", "Grid1 Test4", "Grid1 Test5" };
Grid2Source = new ObservableCollection<string> { "Grid2 Test1", "Grid2 Test2", "Grid2 Test3", "Grid2 Test4" };
Grid3Source = new ObservableCollection<string> { "Grid3 Test1", "Grid3 Test2", "Grid3 Test3" };
}
public void Initialize()
{
RaisePropertyChanged(nameof(Grid1Source));
RaisePropertyChanged(nameof(Grid2Source));
RaisePropertyChanged(nameof(Grid3Source));
}
}
编辑:似乎问题没有将 PreviewKeyDown 事件标记为已处理