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

后端 未结 5 1450
滥情空心
滥情空心 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:22

    Add SelectedItem, SelectedValue in your DataGrid.

    
    

    And in your view model

    private string _selectedValue;
    public string SelectedValue
    {
        get
        {
            return _selectedValue;
        }
        set
        {
            _selectedValue = value;
            NotifyPropertyChanged("SelectedValue");
        }
    }
    
    private DataTable sizeQuantityTable;
    public DataTable SizeQuantityTable
    {
        get
        {
            return sizeQuantityTable;
        }
        set
        {
            sizeQuantityTable = value;
            NotifyPropertyChanged("SizeQuantityTable");
        }
    }
    

    You can use SelectedItem as well, that is preferred.

提交回复
热议问题