How to set selected item of a DataGrid programmatically in WPF with MVVM application?

后端 未结 5 1477
滥情空心
滥情空心 2021-01-02 11:17

I have bound the DataTable to the DataGrid control. How can I set the selected item programmatically ?

Exam

5条回答
  •  鱼传尺愫
    2021-01-02 11:47

    There are a few way to select items in the DataGrid. It just depends which one works best for the situation

    First and most basic is SelectedIndex this will just select the Row at that index in the DataGrid

     
    
    private int _selectedIndex;
    public int SelectedIndex
    {
        get { return _selectedIndex; }
        set { _selectedIndex = value; NotifyPropertyChanged("SelectedIndex"); }
    }
    
    SelectedIndex = 2;
    

    SelectedItem will select the row that matches the row you set

    
    
    private DataRow _selectedRow;
    public DataRow SelectedRow
    {
        get { return _selectedRow; }
        set { _selectedRow = value; NotifyPropertyChanged("SelectedRow");}
    }
    
    SelectedRow = items.First(x => x.whatever == something);
    

    The most common one is SelectedValue with SelectedValuePath set, in this case you set the column you want to select with and then to can select the row by setting the corresponding value

    Edit:

    Here is my test and it is highlighting just fine

    Code:

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
    
            this.SizeQuantityTable = new DataTable();
            DataColumn sizeQuantityColumn = new DataColumn();
            sizeQuantityColumn.ColumnName = "Size Quantity";
            ...................
            ........
    
        }
    
        private string _selectedValue;
        public string SelectionValue 
        {
            get { return _selectedValue; }
            set { _selectedValue = value; NotifyPropertyChanged("SelectionValue"); }
        }
    
        private int _selectedIndex;
        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set { _selectedIndex = value; NotifyPropertyChanged("SelectedIndex"); }
        }
    
        private DataTable sizeQuantityTable;
        public DataTable SizeQuantityTable
        {
            get { return sizeQuantityTable; }
            set { sizeQuantityTable = value; NotifyPropertyChanged("SizeQuantityTable"); }
        }
    
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SelectedIndex = 2;
        }
    
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            SelectionValue = "Blue";
        }
    
        private void NotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }
    }
    

    Xaml:

    
    
        
            
            
                

    Result:

    enter image description here

提交回复
热议问题