开发者

WPF DataGrid programmatic multiple row selection

Is there anything simpler than sample below ? I do have observable collection ( "list" in the code ) bound to DataGrid lstLinks

for开发者_开发问答 (int i = 0; i < list.Count ; i++)
{
    object rowItem = lstLinks.Items[i] ; 
    DataGridRow visualItem =  (DataGridRow)lstLinks.ItemContainerGenerator.ContainerFromItem(rowItem);
    if ( visualItem == null ) break;  
    if (list[i].Changed)
        visualItem.IsSelected = false;
    else
         visualItem.IsSelected = false; 

}


Salam MicMit :)

Yeah, there is a more simple solution, you need just to add the items you want from your bound list to your DataGrid SelectedItems collection, see the code below: [Don't forget to mark as answer if this post solved your problem :)]

<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"},
                    new Employee {Code = "E431", Name = "Zuhair Zein"},
                    new Employee {Code = "E431", Name = "Layla Abdullah"},
                };

        /* 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, add it to your DataGrid SelectedItems collection to
         * add the item to the selected rows
         */
        if (emp != null)
            empDataGrid.SelectedItems.Add(emp);
    }
}


MVVM Require more tender solution,
but it is MVVM and it is fully testable and easy to maintain.

Take a look here Managing multiple selections with MVVM


Yeah, this solution is better than hacking into the DataGrid control, and if you want to select only one row you can also use the following code:

myDataGrid.SelectedItem = item;

where the item is one of the items bound the DataGrid.


Assuming lstLinks is a WPF DataGrid, this adds item at position i to the selected items:

lstLinks.SelectedItems.Add(lstLinks.Items[i]);

To unselect everything:

lstLinks.SelectedItems.Clear();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜