这是我的场景。
我有一个带有 frame
的 MainWindow
。此框架允许我从 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 Customers,Page2
显示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/