[英]WPF DataGrid changing row selection to a cell's TextBox
I have a DataGrid and when the users hits the down arrow it need to move to next row below but focus on a cell that has a textbox in in. I want to not focus on textbox not the actual cell. 我有一个DataGrid,当用户单击向下箭头时,它需要移动到下面的下一行,但要专注于其中具有文本框的单元格。我不想只关注文本框而不是实际单元格。 This is what my DataGrid looks like; 这就是我的DataGrid的样子;
<DataGrid Name="myDataGrid" PreviewKeyDown="myDataGrid_PreviewKeyDown">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Description" IsReadOnly="True" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Right" Text="{Binding Path=Descrip, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, StringFormat=C, ConverterCulture=en-us}" TextAlignment="Right"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Units Counted" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox BorderBrush="Black" BorderThickness="2" FontWeight="Bold" Margin="2" Text="{Binding Path=UnitCounted, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
private void myDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Down)
{
e.Handled = true;
int indx = myDataGrid.SelectedIndex + 1;
myDataGrid.SelectedIndex = indx;
myDataGrid.CurrentCell = new DataGridCellInfo(myDataGrid.Items[indx], myDataGrid.Columns[1])
}
}
Doing this selects the cell not the textbox. 这样做将选择单元格而不是文本框。 If I add this to textbox; 如果我将其添加到文本框;
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"
It adds a cursor to every row textbox but doesn't focus on the textbox. 它将光标添加到每一行文本框,但不将焦点放在文本框上。
thanks 谢谢
edit: Thanks to @Sheridan I am closer with this code; 编辑:感谢@Sheridan,我更接近此代码;
<TextBox
BorderBrush="Black"
BorderThickness="2"
FontWeight="Bold"
Margin="2"
Text="{Binding Path=UnitCounted, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"
TextAlignment="Right"
FocusManager.IsFocusScope="True"
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
Then in my myDataGrid_PreviewKeyDown
adding in myDataGrid.BeginEdit()
allows for it to work. 然后在myDataGrid_PreviewKeyDown
添加myDataGrid.BeginEdit()
使其工作。 The problem is that it seems that BeginEdit never actually ends. 问题在于,BeginEdit似乎从未真正结束。 How do I fix this? 我该如何解决?
I wouldn't mess with the FocusManager.FocusedElement
as you get unknown caveats that it can do like what you are experiencing right now with the BeginEdit
. 我不会对FocusManager.FocusedElement
BeginEdit
,因为您会得到一些未知的警告,它可以像使用BeginEdit
一样立即执行BeginEdit
。
What I did to focus on the TextBox
is simple, in xaml 在xaml中,我专注于TextBox
很简单
<DataGrid Name="myDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Loans}" PreviewKeyDown="UIElement_OnPreviewMouseDown">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Units Counted" Width="Auto">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="sampleTextBox" HorizontalAlignment="Right" Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
And in class file I have 在班级文件中
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
private void UIElement_OnPreviewMouseDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Down) return;
e.Handled = true;
var indx = myDataGrid.SelectedIndex + 1;
myDataGrid.SelectedIndex = indx;
myDataGrid.CurrentCell = new DataGridCellInfo(myDataGrid.Items[indx], myDataGrid.Columns[0]);
var row = this.myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.CurrentCell.Item) as DataGridRow;
var presenter = GetVisualChild<DataGridCellsPresenter>(row);
var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromItem(myDataGrid.CurrentCell.Item);
if (cell != null)
{
var contentPresenter = cell.Content as ContentPresenter;
if (contentPresenter != null)
{
var m = contentPresenter.ContentTemplate.FindName("sampleTextBox", contentPresenter);
((TextBox) m).Focus();
}
}
}
}
Now that'll make the TextBox
be focus when Down
key is pressed every time and changes the cell selection. 现在,每次按下Down
键时,将使TextBox
成为焦点,并更改单元格选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.