2010-11-18 96 views
1

我已經在DataGridTemplateColumn.CellEditing DataTemplate中設置了東西。 我希望當單元格編輯加載並顯示模板時,應將鍵盤焦點賦予模板中的某個控件。Xamly將鍵盤焦點設置爲DataGridTemplateColumn單元中的元素?

考慮這個例子,當你去編輯模式,文本框不是鍵盤聚焦:

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
    <DataGrid Name="dg" AutoGenerateColumns="False"> 
     <DataGrid.Columns> 
     <DataGridTemplateColumn Header="Title"> 
      <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Title}"/> 
      </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
      <DataGridTemplateColumn.CellEditingTemplate> 
      <DataTemplate> 
       <TextBox Text="{Binding Title}"/> 
      </DataTemplate> 
      </DataGridTemplateColumn.CellEditingTemplate> 
     </DataGridTemplateColumn> 
     <DataGridTemplateColumn Header="Value"> 
      <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Note}"/> 
      </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
      <DataGridTemplateColumn.CellEditingTemplate> 
      <DataTemplate> 
       <TextBox Text="{Binding Note}"/> 
      </DataTemplate> 
      </DataGridTemplateColumn.CellEditingTemplate> 
     </DataGridTemplateColumn> 
     </DataGrid.Columns>  
    </DataGrid> 
    </Grid> 
</Window> 

Class MainWindow 

    Private Sub Window_Loaded(ByVal sender As Object, 
          ByVal e As RoutedEventArgs) Handles MyBase.Loaded 

    Dim data As New List(Of Item) From 
     {New Item, 
     New Item, 
     New Item} 
    dg.ItemsSource = data 
    End Sub 

End Class 

Public Class Item 
    Private Shared Number As Integer = 0 
    Sub New() 
    Number += 1 
    End Sub 
    Public Property Title = "Title " & Number.ToString 
    Public Property Note = "Note " & Number.ToString 
End Class 

回答

7

那麼答案竟是簡單,上面的文本框應作如下修改:

<TextBox Text="{Binding Title}" 
    FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/> 
<TextBox Text="{Binding Note}" 
    FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/> 
相關問題