c# - 显示页面时如何触发命令

标签 c# wpf mvvm

这是我的场景。

我有一个带有 frameMainWindow。此框架允许我从 Page1 导航到 Page2 再到 Page3(以任何顺序)。

我需要的是;当显示每个页面时,我需要一个命令被触发

例如:

我的 3 个页面都有 DataGrids

mc:Ignorable="d"
  Title="Page1">

<DataGrid DataContext="{Binding Source={x:Static VM:ViewModel.Instance}}"
          ItemsSource="{Binding CustomerCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          RowDetailsVisibilityMode="VisibleWhenSelected"
          AutoGenerateColumns="True">

</DataGrid>

这些 DataGrids 绑定(bind)到同一 (static) ViewModel 中的同一“Customer”ObservableCollection

    public ObservableCollection<customer> CustomerCollection
    {
        get
        {
            return _customercollection;
        }
        set
        {
            _customercollection = value;
            OnPropertyChanged("CustomerCollection");
        }
    }

这意味着在任何给定的时间点,每个页面都显示相同的客户信息。

现在我希望 Page1 显示active CustomersPage2 显示suspended Customers Page3 显示辞职的客户

我需要在 ViewModel 中为每个页面触发不同的查询。每个查询都特定于页面

但是如何让这个查询在我从一个页面导航到另一个页面时自动触发?

The idea here is to limit how much memory is in use when the application runs by recycling the same ObservableCollections.

最佳答案

我认为您可以将一个命令绑定(bind)到每个页面的加载事件并传递一个命令参数,这取决于您可以选择您的可观察集合。

<Window x:Class="V_Parcel.SplashPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:CommandClass="clr-namespace:V_Parcel"
        xmlns:prop="clr-namespace:V_Parcel.Properties"
        xmlns:vm="clr-namespace:V_Parcel"     
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
       >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding StartButton}" CommandParameter="1" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

关于c# - 显示页面时如何触发命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822855/

相关文章:

c# - 我应该创建单元测试基地吗

wpf - 将列表框项内的命令绑定(bind)到 View 模型父级上的属性

c# - 在哪里创建/获取/缓存 ViewModels?

c# - 在 C# 中分段下载?

c# - 为什么在 Visual C# 中将像素写入图片框时遇到问题?

c# - 使用指向 C# 的指针迁移函数 C++

C# MS-Access SQL INSERT INTO 错误

c# - MVVM Light 和撤消/重做?

c# - Wpf 自定义窗口,Windows 边缘调整大小功能

java - MVVM MediatorLiveData观察者onchanged被调用多次