2015-12-11 118 views
0

我現在面臨的問題標籤與WPF DataGridDataGridTemplateColumn。我需要標籤兩次獲得焦點的控制裝置與DataGridTemplateColumn。我嘗試以下方式與本網站針對此問題規定。但是我的問題仍然沒有解決。WPF Datagrid的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 does not match type of element 'DataGridCell'

請幫我解決我的標籤的焦點問題。

回答

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> 

不應用在數據網格設置CellStyle

<DataGrid CellStyle ="{StaticResource DataGridCellSingleClickEditStyle}"> 
....... 
</DataGrid> 
+0

喜,謝謝你的答覆。我在我的示例應用程序中添加了你的代碼。我遇到了setter屬性的問題。我改變了setter屬性,如下所示。 – user5668833

+0

您是否在上述評論中發佈了評論? – user1672994

+0

嗨,謝謝你的答覆。我在我的示例應用程序中添加了你的代碼。我遇到了setter屬性的問題。我改變了setter屬性,如下所示。 <形式X:鍵= 「DataGridCellSingleClickEditStyle」 的TargetType = 「{x:類型DataGridCell}」> 我正類型 'PBO:DataGridCellSingleClickEditDependency'沒有找到。我在名稱空間PBO下添加了您的類。請幫助我解決我的問題。應該在XAML中定義 – user5668833