在沒有使用MVVM框架導(dǎo)航功能的情況下,要實(shí)現(xiàn)子窗口在主窗口的嵌套,可以通過在主窗口中使用ContentControl
容器控件來完成,子窗口使用用戶控件來構(gòu)建,然后作為內(nèi)容放入到主窗口的ContentControl
中就OK了。
創(chuàng)建導(dǎo)航欄模型
public class MenuModel
{
//導(dǎo)航標(biāo)題
public string ManuTitle { get; set; }
//導(dǎo)航所對(duì)應(yīng)的用戶控件路徑,例如WPFExample.Views.FirstPage
public string TargetView { get; set; }
}
創(chuàng)建主窗口模型
public class MainModel : INotifyPropertyChanged
{
//導(dǎo)航列表
public List<MenuModel> MenuList { get; set; } = new List<MenuModel>();
//嵌入的子窗口的標(biāo)題
public string PageTitle { get; set; }
//嵌入的子窗口對(duì)象
private object _page;
public object Page {
get => _page;
set {
_page = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Page"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
創(chuàng)建命令基礎(chǔ)類型
public class CommandBase : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public Action<object> DoExecute { get; set; }
public void Execute(object parameter)
{
DoExecute?.Invoke(parameter);
}
}
創(chuàng)建主窗口的視圖模型
在主窗口視圖模型中定義窗口的一些業(yè)務(wù)邏輯處理、主窗口模型的創(chuàng)建及初始化。文章來源:http://www.zghlxwxcb.cn/news/detail-804426.html
internal class MainViewModel
{
public MainModel MainModel { get; set; }
public MainViewModel()
{
MainModel = new MainModel();
MainModel.MenuList.Add(new MenuModel
{
ManuTitle = "第一個(gè)子窗口",
TargetView="WPFExample.Views.FirstUserControl"
});
MainModel.MenuList.Add(new MenuModel
{
ManuTitle = "第二個(gè)子窗口",
TargetView = "WPFExample.Views.SecondUserControl"
});
//默認(rèn)顯示第一個(gè)子窗口
MainModel.PageTitle = MainModel.MenuList[0].ManuTitle;
ShowPage(MainModel.MenuList[0].TargetView);
}
public void ShowPage(string targetView)
{
Type type = GetType().Assembly.GetType(targetView);
MainModel.Page = Activator.CreateInstance(type);
}
public CommandBase GuideCommand
{
get
{
return new CommandBase
{
DoExecute = obj =>
{
Type type = GetType().Assembly.GetType(obj.ToString());
MainModel.Page = Activator.CreateInstance(type);
}
};
}
}
}
在xaml中使用文章來源地址http://www.zghlxwxcb.cn/news/detail-804426.html
<Window ......
xmlns:vm="clr-namespace:WPFExample.ViewModels"
......>
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<ItemsControl ItemsSource="{Binding MainModel.MenuList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding ManuTitle}" Tag="{Binding TargetView}" Height="40" Width="100"
Command="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.GuideCommand}"
CommandParameter="{Binding TargetView}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="1" >
</UniformGrid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<ContentControl Grid.Row="0" Grid.Column="1" Content="{Binding MainModel.Page}" Name="cc"/>
</Grid>
</Window>
到了這里,關(guān)于WPF常用技巧-原生子窗口嵌套/切換的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!