我假设您使用的是MVVM框架。(我已经在这里添加了关键元素,以防您没有添加)。
您的主窗口。xaml应该使用“ItemsControl”而不是“Frame”。框架可以工作,但更好的方法是像这样使用ItemsControl:
<!-- Main Frame -->
<Grid Grid.Column="1" Margin="10" Name="MainWindowFrameContent">
<ItemsControl ItemsSource="{Binding Path=MainWindowFrameContent}" >
<!-- This controls the height automatically of the user control -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
在主窗口的构造函数中。cs,我将窗口的DataContext设置为MainViewModel:
using myProject.ViewModel;
public partial class MainWindow : Window
{
MainViewModel mMainViewModel;
public MainWindow()
{
InitializeComponent();
// Initialize MainViewModel and set data context to the VM
mMainViewModel = new MainViewModel();
DataContext = mMainViewModel;
}
}
(我不确定下一部分是否必须是一个可观察的集合,但我已经这样实现了,它似乎工作得很好。唯一的缺点是,我需要在添加新的UserControl之前手动清除ItemsControl)
我的MainViewModel实现了名为“MainWindowFrameContent”的绑定。我的所有用户控件都在MainViewModel中初始化。cs代码。我为每个UserControl都有一个额外的ViewModel,并在将UserControl显示到主窗口之前,将UserControl的DataContext分配给各个ViewModel。
我的MainViewModel.cs:
public class MainViewModel : ObservableObject
{
public MainViewModel()
{
}
// This handles adding framework (UI) elements to the main window frame
ObservableCollection<FrameworkElement> _MainWindowFrameContent = new ObservableCollection<FrameworkElement>();
public ObservableCollection<FrameworkElement> MainWindowFrameContent
{
get
{
return _MainWindowFrameContent;
}
set
{
_MainWindowFrameContent = value;
RaisePropertyChangedEvent("MainWindowFrameContent");
}
}
// This handles opening a generic user control on the main window
// The ICommand implementation allows me to bind the command of a button on the main window to displaying a specific page
public ICommand MainWindowDataEntryView
{
get
{
return new DelegateCommand(_MainWindowDataEntryView);
}
}
void _MainWindowDataEntryView(object obj)
{
DataEntryVM wDataEntryVM = new DataEntryVM();
DataEntryView wDataEntryView = new DataEntryView();
wDataEntryView.DataContext = wDataEntryVM;
MainWindowFrameContent.Clear();
MainWindowFrameContent.Add(wDataEntryView);
}
}
然后你需要确保你有一个ObservableObject。cs作为项目的一部分:
using System.ComponentModel;
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChangedEvent(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
您需要一个DelegateCommand。cs类作为项目的一部分:
using System;
using System.Windows.Input;
public class DelegateCommand : ICommand
{
private readonly Action<object> _action;
public DelegateCommand(Action<object> action)
{
_action = action;
}
public void Execute(object parameter)
{
_action(parameter);
}
public bool CanExecute(object parameter)
{
return true;
}
#pragma warning disable 67
public event EventHandler CanExecuteChanged { add { } remove { } }
#pragma warning restore 67
}
所以,这是一个有点冗长的解释,但一旦您设置了前面的项目,您就可以向主窗口添加一系列按钮。xaml,将每个按钮绑定到一个命令,该命令将新的UserControl添加到ItemsControl。显示UserControl时,可以根据需要添加控件并使用它们。
我希望这有帮助!