gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:08:18 30 4
gpt4 key购买 nike

我在 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

最佳答案

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

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

关于wpf - Xamly 将键盘焦点设置为 DataGridTemplateColumn 单元格中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4218718/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com