![](/img/trans.png)
[英]DataContext on CommandParameter differs from DataContext on Command itself
[英]WPF - Command in datacontext of the window and commandparameter from listview in user control
按照 MVVM 模式,在我的 WPF 应用程序中工作的命令绑定有点麻烦。
我目前拥有的:
MainWindow 与 DataContext MainWindowViewModel。 带有 DataContext SecondPageViewmodel 的 SecondPage(用户控件)。
用户控件仅包含一个名为 StudentListView 的列表视图,该列表视图绑定到 SecondPageViewModel 中的可观察集合。
主 window 有一个文本框,按下回车键将触发我的命令,称为 ReturnPressCommand,它位于 MainWindowViewModel 中。 主 window 还包含 SecondPage 用户控件的实例。
我想做的是能够使用用户控件中的 StudentListView 作为我的 Main window 中命令 ReturnPressCommand 的命令参数。
我该如何做到这一点?
主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>
主要 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;
}
}
}
}
我要执行的命令
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
}
}
}
用户控制
<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>
用户控件的视图模型
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 });
}
}
}
我希望文本框始终可见,但是将来我计划添加更多用户控件,并且它们的可见性将根据用户在应用程序中所做的事情而改变。 现在我想实现一个搜索功能来过滤位于用户控件视图模型中的可观察集合中的学生。 然后用户控件将列出这些过滤的学生。
您可以通过用户控件的名称将 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.