简体   繁体   English

如何使用 WPF 中的 MVVM 设计模式以编程方式选择和设置数据网格行

[英]How to select and set focus a datagrid row programmatically using MVVM design pattern in WPF

I have a simple WPF application that has a data grid.我有一个简单的 WPF 应用程序,它有一个数据网格。 What I want is to select and set focus data grid row once I click a button.我想要的是单击按钮后选择并设置焦点数据网格行。 When the row is selected, I need to change the selected (focused) row using keyboard up/down arrow keys.选择该行后,我需要使用键盘上/下箭头键更改选定(聚焦)的行。 Most impotent thing is, I want to do this in MVVM design pattern.最无能的是,我想在 MVVM 设计模式中做到这一点。 My application is as below.我的申请如下。

Item.cs model is as below: Item.cs模型如下:

public class Item
{
    public string ItemCode { get; set; }
    public string ItemName { get; set; }
    public double ItemPrice { get; set; }

    public Item(string itemCode, string itemName, double itemPrice)
    {
        this.ItemCode = itemCode;
        this.ItemName = itemName;
        this.ItemPrice = itemPrice;
    }
}

ItemViewModel.cs is as below: ItemViewModel.cs如下:

public class ItemsViewModel : INotifyPropertyChanged
{
    private List<Item> _items;

    public List<Item> ItemsCollection
    {
        get { return this._items; }
        set
        {
            _items = value;
            OnPropertyChanged(nameof(ItemsCollection));
        }
    }

    public ItemsViewModel()
    {
        this.ItemsCollection = new List<Item>();
        //Add default items
        this.ItemsCollection.Add(new Item("I001", "Text Book", 10));
        this.ItemsCollection.Add(new Item("I002", "Pencil", 20));
        this.ItemsCollection.Add(new Item("I003", "Bag", 15));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

MainWindowViewModel.cs as below: MainWindowViewModel.cs如下:

public class MainWindowViewModel : INotifyPropertyChanged
{
    public ICommand SelectRow { get; private set; }

    public MainWindowViewModel()
    {
        this.SelectRow = new RelayCommand(this.SelectGridRow, o => true);
    }

    private void SelectGridRow(object param)
    {
        //TODO: Code should goes here
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

I have written following RelayCommand.cs to handle the command binding.我编写了以下RelayCommand.cs来处理命令绑定。

public class RelayCommand : ICommand
{
    #region Fields

    /// <summary>
    /// Encapsulated the execute action
    /// </summary>
    private Action<object> execute;

    /// <summary>
    /// Encapsulated the representation for the validation of the execute method
    /// </summary>
    private Predicate<object> canExecute;

    #endregion // Fields

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the RelayCommand class
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action<object> execute)
        : this(execute, DefaultCanExecute)
    {
    }

    /// <summary>
    /// Initializes a new instance of the RelayCommand class
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        if (canExecute == null)
        {
            throw new ArgumentNullException("canExecute");
        }

        this.execute = execute;
        this.canExecute = canExecute;
    }

    #endregion // Constructors

    #region ICommand Members

    /// <summary>
    /// An event to raise when the CanExecute value is changed
    /// </summary>
    /// <remarks>
    /// Any subscription to this event will automatically subscribe to both 
    /// the local OnCanExecuteChanged method AND
    /// the CommandManager RequerySuggested event
    /// </remarks>
    public event EventHandler CanExecuteChanged
    {
        add
        {
            CommandManager.RequerySuggested += value;
            this.CanExecuteChangedInternal += value;
        }

        remove
        {
            CommandManager.RequerySuggested -= value;
            this.CanExecuteChangedInternal -= value;
        }
    }

    /// <summary>
    /// An event to allow the CanExecuteChanged event to be raised manually
    /// </summary>
    private event EventHandler CanExecuteChangedInternal;

    /// <summary>
    /// Defines if command can be executed
    /// </summary>
    /// <param name="parameter">the parameter that represents the validation method</param>
    /// <returns>true if the command can be executed</returns>
    public bool CanExecute(object parameter)
    {
        return this.canExecute != null && this.canExecute(parameter);
    }

    /// <summary>
    /// Execute the encapsulated command
    /// </summary>
    /// <param name="parameter">the parameter that represents the execution method</param>
    public void Execute(object parameter)
    {
        this.execute(parameter);
    }

    #endregion // ICommand Members

    /// <summary>
    /// Raises the can execute changed.
    /// </summary>
    public void OnCanExecuteChanged()
    {
        EventHandler handler = this.CanExecuteChangedInternal;
        if (handler != null)
        {
            //DispatcherHelper.BeginInvokeOnUIThread(() => handler.Invoke(this, EventArgs.Empty));
            handler.Invoke(this, EventArgs.Empty);
        }
    }

    /// <summary>
    /// Destroys this instance.
    /// </summary>
    public void Destroy()
    {
        this.canExecute = _ => false;
        this.execute = _ => { return; };
    }

    /// <summary>
    /// Defines if command can be executed (default behaviour)
    /// </summary>
    /// <param name="parameter">The parameter.</param>
    /// <returns>Always true</returns>
    private static bool DefaultCanExecute(object parameter)
    {
        return true;
    }
}

I have a ItemView.xaml User control as below:我有一个ItemView.xaml用户控件,如下所示:

<UserControl x:Class="DataGrid_FocusRow.ItemView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:DataGrid_FocusRow"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid>
    <StackPanel Orientation="Vertical">
        <DataGrid x:Name="grdItems" ItemsSource="{Binding ItemsCollection}" AutoGenerateColumns="False" ColumnWidth="*">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Item Code" Binding="{Binding ItemCode}" />
                <DataGridTextColumn Header="Item Name" Binding="{Binding ItemName}" />
                <DataGridTextColumn Header="Item Price" Binding="{Binding ItemPrice}" />
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Grid>

My MainWindow.xaml is as below:我的MainWindow.xaml如下:

<Window x:Class="DataGrid_FocusRow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DataGrid_FocusRow"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<StackPanel>
    <local:ItemView/>

    <Button Command="{Binding SelectRow}">Select Row</Button>
</StackPanel>

Can someone please help me with this.有人可以帮我解决这个问题。

Update: I've already tried by setting "SelectedIndex" and "SelectedItem".更新:我已经尝试过设置“SelectedIndex”和“SelectedItem”。 It selects the row in the data grid.它选择数据网格中的行。 But it does not set focus to the particular row.但它不会将焦点设置到特定行。 Because of that I cannot change the selection by UP/DOWN keys in my keyboard.因此,我无法通过键盘上的 UP/DOWN 键更改选择。

I found a way.我找到了一个方法。 I handled the row selection in following SelectingItemAttachedProperty.cs class我在以下SelectingItemAttachedProperty.cs类中处理了行选择

public class SelectingItemAttachedProperty
{
    public static readonly DependencyProperty SelectingItemProperty = DependencyProperty.RegisterAttached("SelectingItem", typeof(Item), typeof(SelectingItemAttachedProperty), new PropertyMetadata(default(Item), OnSelectingItemChanged));

    public static Item GetSelectingItem(DependencyObject target)
    {
        return (Item)target.GetValue(SelectingItemProperty);
    }

    public static void SetSelectingItem(DependencyObject target, Item value)
    {
        target.SetValue(SelectingItemProperty, value);
    }

    static void OnSelectingItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        var grid = sender as DataGrid;
        if (grid == null || grid.SelectedItem == null)
            return;

        grid.Dispatcher.InvokeAsync(() =>
        {
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.SelectedItem, null);
            var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(grid.SelectedIndex);
            row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        });
    }
}

In the ItemView.xaml user control I'm adding my button and setting the SelectingItemAttachedProperty that we implemented above as below.ItemView.xaml用户控件中,我添加了我的按钮并设置了我们在上面实现的SelectingItemAttachedProperty ,如下所示。

<UserControl x:Class="DataGrid_FocusRow.ItemView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:DataGrid_FocusRow"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <StackPanel Orientation="Vertical">
            <DataGrid x:Name="grdItems" ItemsSource="{Binding ItemsCollection}" AutoGenerateColumns="False" ColumnWidth="*" 
                      SelectedItem="{Binding SelectingItem}" local:SelectingItemAttachedProperty.SelectingItem="{Binding SelectingItem}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Item Code" Binding="{Binding ItemCode}" />
                    <DataGridTextColumn Header="Item Name" Binding="{Binding ItemName}" />
                    <DataGridTextColumn Header="Item Price" Binding="{Binding ItemPrice}" />
                </DataGrid.Columns>
            </DataGrid>
            <Button Command="{Binding SelectRow}">Select Row</Button>
        </StackPanel>
    </Grid>
</UserControl>

Then I'm setting the selected item in SelectGridRow() method in the view model as below然后我在视图模型中的SelectGridRow()方法中设置所选项目,如下所示

public class ItemsViewModel : INotifyPropertyChanged
{
    private Item _selectingItem;
    private List<Item> _items;

    public Item SelectingItem
    {
        get { return this._selectingItem; }
        set
        {
            this._selectingItem = value;
            OnPropertyChanged(nameof(this.SelectingItem));
        }
    }

    public List<Item> ItemsCollection
    {
        get { return this._items; }
        set
        {
            _items = value;
            OnPropertyChanged(nameof(ItemsCollection));
        }
    }

    public ICommand SelectRow { get; private set; }

    public ItemsViewModel()
    {
        this.ItemsCollection = new List<Item>();
        //Add default items
        this.ItemsCollection.Add(new Item("I001", "Text Book", 10));
        this.ItemsCollection.Add(new Item("I002", "Pencil", 20));
        this.ItemsCollection.Add(new Item("I003", "Bag", 15));

        this.SelectRow = new RelayCommand(this.SelectGridRow, o => true);
    }

    private void SelectGridRow(object param)
    {
        this.SelectingItem = this.ItemsCollection[0];
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM