如何使用Datagrid.SelectedItem
以编程方式选择行?
我是不是必须首先创建一个DataGridRow
对象的IEnumerable
并将匹配的行传给这个SelectedItem
属性,或者我该怎么做呢?
编辑:
在选择行之前,我需要首先将第一个列单元格的单元格内容与TextBox.Text
匹配。
发布于 2009-12-30 03:40:06
我的代码遍历datagrid
第一列的单元格,检查单元格内容是否等于textbox.text
值,然后选择该行。
for (int i = 0; i < dataGrid.Items.Count; i++)
{
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock;
if (cellContent != null && cellContent.Text.Equals(textBox1.Text))
{
object item = dataGrid.Items[i];
dataGrid.SelectedItem = item;
dataGrid.ScrollIntoView(item);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
break;
}
}
发布于 2011-03-30 16:07:15
您不需要遍历DataGrid
行,您可以使用更简单的解决方案来实现您的目标。为了匹配您的行,您可以遍历绑定到DataGrid.ItemsSource
属性的集合,然后以编程方式将该项分配给DataGrid.SelectedItem
属性,或者,如果您希望允许用户选择多行,则可以将其添加到DataGrid.SelectedItems
集合。请参见以下代码:
<Window x:Class="ProgGridSelection.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" Loaded="OnWindowLoaded">
<StackPanel>
<DataGrid Name="empDataGrid" ItemsSource="{Binding}" Height="200"/>
<TextBox Name="empNameTextBox"/>
<Button Content="Click" Click="OnSelectionButtonClick" />
</StackPanel>
public partial class MainWindow : Window
{
public class Employee
{
public string Code { get; set; }
public string Name { get; set; }
}
private ObservableCollection<Employee> _empCollection;
public MainWindow()
{
InitializeComponent();
}
private void OnWindowLoaded(object sender, RoutedEventArgs e)
{
// Generate test data
_empCollection =
new ObservableCollection<Employee>
{
new Employee {Code = "E001", Name = "Mohammed A. Fadil"},
new Employee {Code = "E013", Name = "Ahmed Yousif"},
new Employee {Code = "E431", Name = "Jasmin Kamal"},
};
/* Set the Window.DataContext, alternatively you can set your
* DataGrid DataContext property to the employees collection.
* on the other hand, you you have to bind your DataGrid
* DataContext property to the DataContext (see the XAML code)
*/
DataContext = _empCollection;
}
private void OnSelectionButtonClick(object sender, RoutedEventArgs e)
{
/* select the employee that his name matches the
* name on the TextBox
*/
var emp = (from i in _empCollection
where i.Name == empNameTextBox.Text.Trim()
select i).FirstOrDefault();
/* Now, to set the selected item on the DataGrid you just need
* assign the matched employee to your DataGrid SeletedItem
* property, alternatively you can add it to your DataGrid
* SelectedItems collection if you want to allow the user
* to select more than one row, e.g.:
* empDataGrid.SelectedItems.Add(emp);
*/
if (emp != null)
empDataGrid.SelectedItem = emp;
}
}
发布于 2011-06-08 08:17:55
我已经找到了类似问题的解决方案,也许我的方法会对你和任何面对它的人有所帮助。
我在XAML DataGrid定义中使用了SelectedValuePath="id"
,在编程上我唯一需要做的就是将DataGrid.SelectedValue
设置为所需的值。
我知道这个解决方案有利有弊,但在特定情况下是快速和简单的。
诚挚的问候
Marcin
https://stackoverflow.com/questions/1976087
复制相似问题