简体   繁体   English

WPF - window 的数据上下文中的命令和来自用户控件中的列表视图的命令参数

[英]WPF - Command in datacontext of the window and commandparameter from listview in user control

I am having a little trouble getting the bindings of a command working in my WPF-application, following the MVVM pattern.按照 MVVM 模式,在我的 WPF 应用程序中工作的命令绑定有点麻烦。

What I currently have:我目前拥有的:

MainWindow with DataContext MainWindowViewModel. MainWindow 与 DataContext MainWindowViewModel。 SecondPage (user control) with DataContext SecondPageViewmodel.带有 DataContext SecondPageViewmodel 的 SecondPage(用户控件)。

The user control only simply contains a listview called StudentListView binding to an observable collection in SecondPageViewModel.用户控件仅包含一个名为 StudentListView 的列表视图,该列表视图绑定到 SecondPageViewModel 中的可观察集合。

The main window has a textbox where pressing the enter key will trigger my command, called ReturnPressCommand which is located in the MainWindowViewModel.主 window 有一个文本框,按下回车键将触发我的命令,称为 ReturnPressCommand,它位于 MainWindowViewModel 中。 The main window also contains an instance of the SecondPage user control.主 window 还包含 SecondPage 用户控件的实例。

What I would like to do is be able to use the StudentListView from the user control as my command parameter for the command ReturnPressCommand in my Main window.我想做的是能够使用用户控件中的 StudentListView 作为我的 Main window 中命令 ReturnPressCommand 的命令参数。

How do I accomplish this?我该如何做到这一点?

The main window:主window:

<Window x:Class="MVVMTEST.Views.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:MVVMTEST.ViewModels"
        xmlns:control="clr-namespace:MVVMTEST.Views"
        mc:Ignorable="d"
        Title="MainWindow" Height="800" Width="1600" WindowStartupLocation="CenterScreen">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <TextBox Grid.Column="0" Grid.Row="0" Width="120" Height="20">
            <TextBox.InputBindings>
                <KeyBinding Key="Return" 
                            Command="{Binding Path=ReturnPressCommand}" 
                            CommandParameter="{}"/>
            </TextBox.InputBindings>
        </TextBox>

        <control:SecondPage x:Name="SecondPageX" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Width="auto" Visibility="Visible"></control:SecondPage>
    </Grid>
</Window>

The viewmodel for the main window主要 window 的视图模型

using MVVMTEST.Commands;
using System.Windows.Input;

namespace MVVMTEST.ViewModels
{
    class MainWindowViewModel
    {
        public MainWindowViewModel()
        {
            
        }

        private ICommand _returnPressCommand = null;
        public ICommand ReturnPressCommand
        {
            get
            {
                if (_returnPressCommand == null)
                    _returnPressCommand = new ReturnPressCommand();
                return _returnPressCommand;
            }
        }
    }
}

The command I want to execute我要执行的命令

namespace MVVMTEST.Commands
{
    class ReturnPressCommand : CommandBase
    {
        public override bool CanExecute(object parameter)
        {
            return true;
        }

        public override void Execute(object parameter)
        {
            // Do operations with the observable collection
        }
    }
}

The user control用户控制

<UserControl x:Class="MVVMTEST.Views.SecondPage"
             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:MVVMTEST.ViewModels"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="1600">

    <UserControl.DataContext>
        <local:SecondPageViewModel />
    </UserControl.DataContext>

    <Grid Background="Green">
        <ListView Grid.Column="0" Grid.Row="0" x:Name="StudentListView" ItemsSource="{Binding Students}">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name"></GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding Age}" Header="Age"></GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding Gpa}" Header="Gpa"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</UserControl>

The viewmodel for the user control用户控件的视图模型

using System.Collections.Generic;
using System.Collections.ObjectModel;
using MVVMTEST.Models;

namespace MVVMTEST.ViewModels
{
    public class SecondPageViewModel
    {
        public IList<Student> Students { get; } = new ObservableCollection<Student>();
        public SecondPageViewModel()
        {
            Students.Add(new Student { Name = "Firstname Lastname", Age = 25, Gpa = 0.0 });
        }
    }
}

I want the textbox to be visible at all times, however in the future I am planning on adding more user controls and their visibility will change depending on what the user is doing in the application.我希望文本框始终可见,但是将来我计划添加更多用户控件,并且它们的可见性将根据用户在应用程序中所做的事情而改变。 Right now I want to implement a search feature to filter the students in the observable collection located in the viewmodel for the user control.现在我想实现一个搜索功能来过滤位于用户控件视图模型中的可观察集合中的学生。 The user control will then list these filtered students.然后用户控件将列出这些过滤的学生。

You can bind the CommandParameter to the collection via the name of the usercontrol.您可以通过用户控件的名称将 CommandParameter 绑定到集合。

<TextBox Grid.Column="0" Grid.Row="0" Width="120" Height="20">
            <TextBox.InputBindings>
                <KeyBinding Key="Return" 
                            Command="{Binding Path=ReturnPressCommand}" 
                            CommandParameter="{Binding DataContext.Students, ElementName=SecondPageX}" />
            </TextBox.InputBindings>
</TextBox>

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

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