您可以在枚举更改的基础上在主窗口资源中创建数据模板,并将适当的数据模板绑定到控件模板(在网格内,您希望在其中显示),而不是绑定可见性
以下是一个大概的想法。
在您的mainwindow.xaml内
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="DTLoginView">
<local:LoginView />
</DataTemplate>
<DataTemplate x:Key="DTPageOne">
<local:PageOne />
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
现在,在您的mainwindow视图模型内部,执行一些逻辑,并根据该逻辑存储页面的值。您当前的页面属性应实现INotifyPropertyChanged,其外观应类似于以下内容。 (注意:我为基本的MVVM接线添加了Haley.Flipper.MVVM nuget包(免责声明:Haley nuget包由我开发)。您可以实现自己的INotifyPropertyChanged或使用某些MVVM库)
private PageType _CurrentPage;
public PageType CurrentPage
{
get { return _CurrentPage; }
set { _CurrentPage = value; onPropertyChanged(); }
}
在XAML内部的MainWindow中。 (在有网格的地方)
<Grid x:Name="grdPages" DataContext={Binding}>
<ContentControl >
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=grdPages,Path=DataContext.CurrentPage,NotifyOnSourceUpdated=True,UpdateSourceTrigger=PropertyChanged}" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource DTLoginView}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=grdPages,UpdateSourceTrigger=PropertyChanged}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource DTPageOne}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
如果您看一下上面的xaml代码,我对数据触发绑定的值为“ 0”“ 1”,因为枚举应具有0、1、2、3,依此类推。但是,您也可以将枚举直接绑定为值。进行一些搜索,您可以轻松找到答案。
当前页面的属性(枚举值)应由某些逻辑(由您实现)设置。完成后,它将自动触发向xaml的通知。
希望这可以在某种程度上帮助您。
,
与使用Frame
相对,最简单,最轻便的方法是为每个页面创建一个视图模型。然后创建一个包含所有页面并管理其选择的主视图模型。 ContentControl
将使用分配给DataTemplate
属性的ContentControl.ContentTemplate
或在多页方案中显示DataTemplateSelector
分配给ContentControl.ContentTemplateSelector
或隐式模板的视图模型通过仅定义DataTemplate.DataType
而没有Key
属性的方式:
风景
MainWindow.xaml
<Window>
<Window.DataContext>
<MainViewModel x:Key="MainViewModel" />
</Window.DataContext>
<Window.Resources>
<!--
The templates for the view of each page model.
Can be moved to dedicated files.
-->
<DataTemplate DataType="{x:Type LoginViewModel}">
<Border Background="Coral">
<local:LoginView />
</Border>
</DataTemplate>
<DataTemplate DataType="{x:Type PageOneViewModel}">
<Border Background="Red">
<local:PageOne />
</Border>
</DataTemplate>
<DataTemplate DataType="{x:Type PageTwoViewModel}">
<Border Background="DeepSkyBlue">
<local:PageTwo />
</Border>
</DataTemplate>
<DataTemplate DataType="{x:Type PageThreeViewModel}">
<Border Background="Yellow">
<local:PageThree />
</Border>
</DataTemplate>
</Window.Resources>
<StackPanel>
<Button Content="Load Login Page"
Command="{Binding SelectPageFromIndexCommand}"
CommandParameter="0" />
<Button Content="Load Page One"
Command="{Binding SelectPageFromIndexCommand}"
CommandParameter="1" />
<Button Content="Load Next Page"
Command="{Binding SelectNextPageCommand}" />
<!-- The actual page control -->
<ContentControl Content="{Binding SelectedPage}" />
</StackPanel>
</Window>
视图模型
MainViewModel.cs
class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
this.Pages = new ObservableCollection<BaseViewModel>()
{
new LoginViewModel(),new PageOneViewModel(),new PageTwoViewModel(),new PageThreeViewModel()
};
// Show startup page
this.SelectedPage = this.Pages.First();
}
// Define the Execute and CanExecute delegates for the command
// and pass them to the constructor
public ICommand SelectPageFromIndexCommand => new SelectPageCommand(
param => this.SelectedPage = this.Pages.ElementAt(int.Parse(param as string)),param => int.TryParse(param as string,out int index));
// Define the Execute and CanExecute delegates for the command
// and pass them to the constructor
public ICommand SelectNextPageCommand => new SelectPageCommand(
param => this.SelectedPage = this.Pages.ElementAt(this.Pages.IndexOf(this.SelectedPage) + 1),param => this.Pages.IndexOf(this.SelectedPage) + 1 < this.Pages.Count);
private BaseViewModel selectedPage;
public BaseViewModel SelectedPage
{
get => this.selectedPage;
set
{
if (object.Equals(value,this.selectedPage))
{
return;
}
this.selectedPage = value;
OnPropertyChanged();
}
}
private ObservableCollection<BaseViewModel> pages;
public ObservableCollection<BaseViewModel> Pages
{
get => this.pages;
set
{
if (object.Equals(value,this.pages))
{
return;
}
this.pages = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
}
SelectPageCommand.cs
class SelectPageCommand : ICommand
{
public SelectPageCommand(Action<object> executeDelegate,Predicate<object> canExecuteDelegate)
{
this.ExecuteDelegate = executeDelegate;
this.CanExecuteDelegate = canExecuteDelegate;
}
private Predicate<object> CanExecuteDelegate { get; }
private Action<object> ExecuteDelegate { get; }
#region Implementation of ICommand
public bool CanExecute(object parameter) => this.CanExecuteDelegate?.Invoke(parameter) ?? false;
public void Execute(object parameter) => this.ExecuteDelegate?.Invoke(parameter);
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
#endregion
}
页面模型
BaseViewModel.cs
// Base type for all pages
abstract class BaseViewModel : INotifyPropertyChanged
{
}
LoginViewModel.cs
// BaseViewModel implementation.
// Consider to introduce dedicated abstract class Page which extends BaseViewModel
class LoginViewModel : BaseViewModel
{
// Implementation
}
PageOneViewModel.cs
// BaseViewModel implementation.
// Consider to introduce dedicated abstract class Page which extends
class PageOneViewModel : BaseViewModel
{
// Implementation
}
PageTwoViewModel.cs
// BaseViewModel implementation.
// Consider to introduce dedicated abstract class Page which extends
class PageTwoViewModel : BaseViewModel
{
// Implementation
}
PageThreeViewModel.cs
// BaseViewModel implementation.
// Consider to introduce dedicated abstract class Page which extends
class PageThreeViewModel : BaseViewModel
{
// Implementation
}
本文链接:https://www.f2er.com/3105631.html