我正面临着WPF DataGrid
DataGridTemplateColumn
的标签问题。我需要选中两次才能将注意力集中在DataGridTemplateColumn
上的控件上。我按照以下方式尝试本网站针对此问题。但我的问题仍未解决。
<my:DataGrid AutoGenerateColumns="False" Name="dgCB" Margin="8,32,0,0" SelectionMode="Single" GridLinesVisibility="All" FontSize="13" SelectionUnit="Cell"
KeyboardNavigation.TabNavigation="Continue" CanUserAddRows="False" CanUserDeleteRows="False"
EnableColumnVirtualization="True" VerticalAlignment="Top"
RowDetailsVisibilityMode="VisibleWhenSelected"
IsSynchronizedWithCurrentItem="True"
CanUserSortColumns="False"
CanUserReorderColumns="False"
CanUserResizeColumns="True"
CanUserResizeRows="True"
HorizontalAlignment="Left"
Width="964"
Height="416">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Tis">
<my:DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
</Style>
</my:DataGridTemplateColumn.CellStyle>
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Name="txtC" PBOValidation:TextBoxMaskBehavior.Mask="Integer" LostFocus="txtC_LostFocus" Text="{Binding Path=TIME}" GotKeyboardFocus="txtC_GotKeyboardFocus"></TextBox>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
在代码隐藏文件中,我编写了如下的事件处理程序。
private void txtC_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
try
{
((TextBox)sender).SelectAll();
}
catch { }
}
private void txtC_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(((System.Windows.Controls.TextBox)(sender)).Text.Trim()))
{
((System.Windows.Controls.TextBox)(sender)).Text = 0.ToString();
}
}
但我的问题仍然没有得到解决,我收到了以下错误:
'DataGridCell'targettype与元素'DataGridCell'
的类型不匹配
请帮我解决我的标签焦点问题。
答案 0 :(得分:0)
您应该在DataGrid
上定义一个行为,该行为专注于首次点击时点击的单元格。以下行为符合要求:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
/// <summary>
/// Defines a dependency property to allow the dataGridCell to become editable in mouse single click .
/// </summary>
public class DataGridCellSingleClickEditDependency : DependencyObject
{
/// <summary>
/// The is allow single click edit property
/// </summary>
public static readonly DependencyProperty IsAllowSingleClickEditProperty = DependencyProperty.RegisterAttached("IsAllowSingleClickEdit", typeof(bool), typeof(DataGridCellSingleClickEditDependency), new PropertyMetadata(false, IsAllowSingleClickEditChanged));
/// <summary>
/// Gets or sets a value indicating whether this instance is allow single click edit.
/// </summary>
/// <value>
/// <c>true</c> if this instance is allow single click edit; otherwise, <c>false</c>.
/// </value>
public bool IsAllowSingleClickEdit
{
get
{
return (bool)this.GetValue(IsAllowSingleClickEditProperty);
}
set
{
this.SetValue(IsAllowSingleClickEditProperty, value);
}
}
/// <summary>
/// Determines whether [is allow single click edit changed] [the specified sender].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void IsAllowSingleClickEditChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
DataGridCell dataGridCell = sender as DataGridCell;
if (dataGridCell != null)
{
if (e.NewValue.Equals(true))
{
dataGridCell.GotFocus += DataGridCellGotFocusHandler;
}
else
{
dataGridCell.GotFocus -= DataGridCellGotFocusHandler;
}
}
}
/// <summary>
/// Finds the visual parent.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="element">The element.</param>
/// <returns></returns>
private static T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null)
{
return correctlyTyped;
}
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}
/// <summary>
/// Handles the GotFocus event of the AssociatedObject control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private static void DataGridCellGotFocusHandler(object sender, RoutedEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
{
if (!cell.IsFocused)
{
cell.Focus();
}
DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
if (dataGrid != null)
{
dataGrid.BeginEdit(e);
if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
{
if (!cell.IsSelected)
{
cell.IsSelected = true;
}
}
else
{
DataGridRow row = FindVisualParent<DataGridRow>(cell);
if (row != null && !row.IsSelected)
{
row.IsSelected = true;
}
}
}
}
}
}
现在要附加DataGrid
,您可以为DataGridCell
<Style x:Key="DataGridCellSingleClickEditStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="localBehaviors:DataGridCellSingleClickEditDependency.IsAllowSingleClickEdit" Value="True" />
</Style>
不要在DataGrid上应用设置CellStyle
<DataGrid CellStyle ="{StaticResource DataGridCellSingleClickEditStyle}">
.......
</DataGrid>