如何将焦点设置到 WPF 工具包数据网格的特定单元格
我正在使用 WPF 工具包提供的 DataGrid 控件来显示产品列表及其 OpenStock、说明等。在此 DataGrid 中,我将 OpenStock 列设置为可编辑,其余部分不可编辑。现在我想要的是,当我的这个窗口加载时,我想将键盘焦点设置为 OpenStock 列的第一个单元格,如果可能的话在编辑模式下。我搜索了两天,最后发布在这里。
任何帮助或代码示例都会有帮助。
<my:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Margin="2,2,2,55"
x:Name="dataGrid1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="White"
AlternatingRowBackground="AliceBlue" AlternationCount="2" SelectionMode="Single"
SelectionUnit="Cell" BorderThickness="0" IsTabStop="True">
<my:DataGrid.Resources>
<Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
<Setter Property="Padding" Value="-2"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</my:DataGrid.Resources>
<my:DataGrid.Columns>
<my:DataGridTextColumn Width="60" FocusManager.IsFocusScope="False" Binding="{Binding Path=pCode}" Header="Code" IsReadOnly="True" />
<my:DataGridTextColumn Width="150" Binding="{Binding pName}" Header="Description" IsReadOnly="True" />
<my:DataGridTextColumn Width="120" Binding="{Binding CloseStock}" Header="Closing Stock" IsReadOnly="True" />
<my:DataGridTextColumn Width="100" Binding="{Binding OpenStock, ValidatesOnExceptions=True}" Header="Open Stock"
EditingElementStyle="{StaticResource errorStyle}"/>
<my:DataGridTextColumn Width="150" Binding="{Binding YrlyOpenStock}" Header="Yearly Opening" IsReadOnly="True" />
</my:DataGrid.Columns>
</my:DataGrid>
多谢........
I am using WPF toolkit provided DataGrid control to display product list along with its OpenStock, Description etc. In this DataGrid i have set OpenStock column to editable and rest are non-editable. What i want now when my this windows loads, I want to set keyboard focus to first cell of OpenStock column and if possible in edit mode. I searched this for 2 days and finally posting here.
any help or code sample would be helpful.
<my:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Margin="2,2,2,55"
x:Name="dataGrid1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="White"
AlternatingRowBackground="AliceBlue" AlternationCount="2" SelectionMode="Single"
SelectionUnit="Cell" BorderThickness="0" IsTabStop="True">
<my:DataGrid.Resources>
<Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
<Setter Property="Padding" Value="-2"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Yellow"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</my:DataGrid.Resources>
<my:DataGrid.Columns>
<my:DataGridTextColumn Width="60" FocusManager.IsFocusScope="False" Binding="{Binding Path=pCode}" Header="Code" IsReadOnly="True" />
<my:DataGridTextColumn Width="150" Binding="{Binding pName}" Header="Description" IsReadOnly="True" />
<my:DataGridTextColumn Width="120" Binding="{Binding CloseStock}" Header="Closing Stock" IsReadOnly="True" />
<my:DataGridTextColumn Width="100" Binding="{Binding OpenStock, ValidatesOnExceptions=True}" Header="Open Stock"
EditingElementStyle="{StaticResource errorStyle}"/>
<my:DataGridTextColumn Width="150" Binding="{Binding YrlyOpenStock}" Header="Yearly Opening" IsReadOnly="True" />
</my:DataGrid.Columns>
</my:DataGrid>
thanks alot........
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要将当前单元格设置为要编辑的单元格,然后调用
如果您在 XAML 中为 DataGridTextColumn 指定一个名称,则可以使用该名称而不是
Columns[3]
。You need to set the current cell to the one you want edited and then call BeginEdit in your Loaded handler:
If you give your DataGridTextColumn a name in XAML you can use that name rather than
Columns[3]
.使用此代码将滚动视图移动到特定单元格:
Use this code to move the scroll view to a specific cell:
这对我有用:
这将选择您想要将焦点放在的单元格。
DataGridCellInfo 对象提供有关单元格以及与单元格关联的数据项的信息。当 DataGrid 控件获取单元格(例如在 CurrentCell 或 SelectedCells 属性中)时,使用它来代替对实际 DataGridCell 对象的引用。检查此处了解更多信息。
This worked for me:
This will select the cell you want to put the focus on.
A DataGridCellInfo object provides information about a cell and the data item that is associated with the cell. It is used instead of a reference to the actual DataGridCell object when the DataGrid control gets a cell, for example in the CurrentCell or SelectedCells properties. Check here for more info.
我在 DataGridTemplateColumn 的 DataTemplate 中有带有 TextBox 的数据网格。
另外,我使用 Enter 而不是 Tab 来聚焦 TextBox
我通过结合以下代码解决了焦点问题:
I have datagrid with TextBox in DataTemplate of DataGridTemplateColumn.
Also i am using Enter instead of Tab to focus TextBox
I solve focus problem with combination of this code:
使用下面的函数,它会起作用。
Use below function, it will work.