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

后端 未结 5 1449
滥情空心
滥情空心 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.

    <DataGrid 
            ItemsSource="{Binding SizeQuantityTable}"
            AutoGenerateColumns="True" 
            SelectedValue ="{Binding SelectedValue}"
            Margin="0,0,0,120" />
    

    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.

    0 讨论(0)
  • 2021-01-02 11:27

    I just had the same problem. I saw that the item of a datagrid was selected correctly at design time, but not at runtime. (By the way, I create the instance of the view model in the xaml).

    I could solve this problem by moving the code to programmatically set the selected item from the view models constructor to a different method in the view model and then calling this method in the loaded event of the window (or usercontrol).

    Obviously the view is not completely done initializing itself when the view models constructor is called. This can be avoided by not coding in the view models constructor.

    View (xaml):

    <Window x:Class="MyWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Test" 
            xmlns:viewModel="clr-namespace:ViewModels" Loaded="Window_Loaded">
        <Window.DataContext>
            <viewModel:ExampleViewModel/>
        </Window.DataContext>
    

    View (code behind):

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ((ExampleViewModel)this.DataContext).LoadData();
        }
    

    If you do not like setting up the Loaded event in the code behind, you can also do it in xaml (references to "Microsoft.Expression.Interactions" and "System.Windows.Interactivity" are needed):

        <Window x:Class="MyWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Test" 
            xmlns:viewModel="clr-namespace:ViewModels">
        <Window.DataContext>
            <viewModel:ExampleViewModel/>
        </Window.DataContext>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Loaded">
                <ei:CallMethodAction TargetObject="{Binding}" MethodName="LoadData"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    

    In each case, you call the LoadData method in the ViewModel:

    public class ExampleViewModel
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public ExampleViewModel()
        {
            // Do NOT set selected item here
        }
    
    
        public void LoadData()
        {
            // Set selected item here
        }
    
    0 讨论(0)
  • 2021-01-02 11:32

    For anyone using Observable Collections you may find this solution useful.

    The SelectedModelIndex property simply returns the index of the SelectedModel from the ItemSource collection. I've found setting the SelectedIndex along with the SelectedItem highlights the row in the DataGrid.

        private ObservableCollection<Model> _itemSource
        public ObservableCollection<Model> ItemSource
        {
            get { return _itemSource; }
            set 
            { 
                _itemSource = value; 
                OnPropertyChanged("ItemSource"); 
            }
        }
    
        // Binding must be set to One-Way for read-only properties
        public int SelectedModelIndex
        {
            get 
            {
                if (ItemSource != null && ItemSource.Count > 0)
                    return ItemSource.IndexOf(SelectedModel);
                else
                    return -1;
            }            
        }
    
        private Model _selectedModel;
        public Model SelectedModel
        {
            get { return _selectedModel; }
            set 
            {
                _selectedModel = value;
                OnPropertyChanged("SelectedModel"); 
                OnPropertyChanged("SelectedModelIndex");               
            }                
        }
    
    0 讨论(0)
  • 2021-01-02 11:43

    You can always use SelectedItem property and bind it against row, as such:

    SelectedItem="{Binding ActiveRow}"
    

    and in ViewModel do:

    ActiveRow = secondRow;
    
    0 讨论(0)
  • 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

     <DataGrid SelectedIndex="{Binding SelectedIndex}" />
    
    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

    <DataGrid SelectedItem="{Binding SelectedRow}" />
    
    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

    <DataGrid SelectedValuePath="Size Quantity" SelectedValue="{Binding SelectionValue}" 
    
    private string _selectedValue
    public string SelectionValue 
    {
        get { return _selectedValue; }
        set { _selectedValue = value; NotifyPropertyChanged("SelectionValue"); }
    }
    
    SelectionValue = "Blue";
    

    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:

    <Window x:Class="WpfApplication21.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="202" Width="232" Name="UI">
    
        <Grid DataContext="{Binding ElementName=UI}">
            <DataGrid SelectedValuePath="Size Quantity"        
                      SelectedValue="{Binding SelectionValue}" 
                      SelectedIndex="{Binding SelectedIndex}"
                      ItemsSource="{Binding SizeQuantityTable}"
                      AutoGenerateColumns="True" 
                      Margin="0,0,0,41" />
            <StackPanel Orientation="Horizontal" Height="37" VerticalAlignment="Bottom" >
                <Button Content="SelectedIndex" Height="26"  Width="107" Click="Button_Click_1"/>
                <Button Content="SelectedValue" Height="26"  Width="107" Click="Button_Click_2"/>
            </StackPanel>
        </Grid>
    </Window>
    

    Result:

    enter image description here

    0 讨论(0)
提交回复
热议问题