Xamly 将键盘焦点设置为 DataGridTemplateColumn 单元格中的元素?

发布于 2024-10-02 20:18:05 字数 2257 浏览 8 评论 0原文

我已经在 DataGridTemplateColumn.CellEditing DataTemplate 中设置了一些内容。 我希望当单元格编辑加载并显示模板时,键盘焦点应该给予模板中的某个控件。

考虑这个例子,当您进入编辑模式时,文本框不是以键盘为中心的:

<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

I've set stuff in a DataGridTemplateColumn.CellEditing DataTemplate.
I want that when the cell editing loads and shows the template, keyboard-focus should be given to a certain control in the template.

Consider this example, when you go edit mode, the textbox is not keyboard-focused:

<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱着落日 2024-10-09 20:18:05

答案其实很简单,上面的文本框应该更改如下:

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

Well answer was actually simple, the above textboxes should be changed as follows:

<TextBox Text="{Binding Title}"
  FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
<TextBox Text="{Binding Note}"
  FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文