菜單導(dǎo)航
添加文件與文件夾
- 添加文件夾
? ./Extensions
-
添加文件[類(lèi)型:用戶控件]
./Views/IndexView.xaml
./Views/MemoView.xaml
./Views/TodoView.xaml
./Views/SettingsView.xaml
./ViewModels/IndexViewModel.cs
./ViewModels/IndexViewModel.cs
./ViewModels/IndexViewModel.cs
./ViewModels/IndexViewModel.cs
./Extensions/PrismManager.cs
建立View與ViewModel關(guān)聯(lián)
-
App.xaml.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<IndexView, IndexViewModel>(); containerRegistry.RegisterForNavigation<TodoView, TodoViewModel>(); containerRegistry.RegisterForNavigation<MemoView, MemoViewModel>(); containerRegistry.RegisterForNavigation<SettingsView, SettingsViewModel>(); }
添加區(qū)域
定義區(qū)域名字
-
PrismManager.cs
namespace Mytodo.Extensions { public static class PrismManager { public static readonly string MainViewRegionName = "MainViewRegion"; //定義Nanme變量 } }
注冊(cè)區(qū)域,綁定名字
-
MainView.xaml
-
定義命名空間
xmlns:ext="clr-namespace:Mytodo.Extensions"
-
注冊(cè)控件區(qū)域
<materialDesign:DrawerHost.LeftDrawerContent> <DockPanel> <materialDesign:ColorZone...> <ContentControl prism:RegionManager.RegionName="{x:Static ext:PrismManager.MainViewRegionName}" /> </DockPanel>
-
添加導(dǎo)航
添加導(dǎo)航命令與變量
- MainView.xaml.cs
private readonly IRegionManager regionManager; //導(dǎo)航變量
public DelegateCommand<MenuBar> NavigateCmd { get; private set; } //導(dǎo)航命令
初始化導(dǎo)航命令
-
MainView.xaml.cs
public MainViewModel(IRegionManager regm) { MenuBars=new ObservableCollection<MenuBar>(); CreatMenuBar(); // this.regionManager = regm; NavigateCmd = new DelegateCommand<MenuBar>(Navigate); }
注意:初始化應(yīng)在構(gòu)造函數(shù)中
綁定導(dǎo)航命令
添加行為命名空間
- main.xaml
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
添加行為,綁定導(dǎo)航命令
- MainView.xmal
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding NavigateCmd}" CommandParameter="{Binding ElementName=menuBar, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
? 完整代碼為:
<ListBox
x:Name="menuBar"
HorizontalContentAlignment="Stretch"
ItemContainerStyle="{StaticResource MyListboxItemStyle}"
ItemsSource="{Binding MenuBars}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding NavigateCmd}" CommandParameter="{Binding ElementName=menuBar, Path=SelectedItem}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="{Binding Icon}" />
<TextBlock Margin="10,0" Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
添加導(dǎo)航歷史功能
添加命令定義
- MainviewModel.cs
public DelegateCommand GoBackCmd { get; private set; }
public DelegateCommand GoFwrdCmd { get; private set; }
添加導(dǎo)航歷史變量
- MainviewModel.cs
private IRegionNavigationJournal journal; //導(dǎo)航歷史
初始化命令
- MainviewModel.cs的構(gòu)造函數(shù)中添加
//實(shí)例化命令
GoBackCmd = new DelegateCommand(() =>
{
if (journal != null && journal.CanGoBack)
journal.GoBack();
});
GoFwrdCmd = new DelegateCommand(() =>
{
if (journal != null && journal.CanGoForward)
journal.GoForward();
});
綁定命令
-
MainView.xaml文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-602976.html
<Button Margin="24,0,0,0" materialDesign:RippleAssist.Feedback="{Binding RelativeSource={RelativeSource Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}" Command="{Binding GoFwrdCmd}" Content="{materialDesign:PackIcon Kind=ArrowLeft, Size=24}" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" Style="{StaticResource MaterialDesignToolButton}" ToolTip="Previous Item" /> <Button Margin="16,0,0,0" materialDesign:RippleAssist.Feedback="{Binding RelativeSource={RelativeSource Self}, Path=Foreground, Converter={StaticResource BrushRoundConverter}}" Command="{Binding GoBackCmd}" Content="{materialDesign:PackIcon Kind=ArrowRight, Size=24}" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" Style="{StaticResource MaterialDesignToolButton}" ToolTip="Next Item" />
添加自動(dòng)關(guān)掉側(cè)欄代碼
-
MainView.xaml.cs文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-602976.html
menuBar.SelectionChanged += (s, e) => { drawerHost.IsLeftDrawerOpen = false; };
到了這里,關(guān)于WPF實(shí)戰(zhàn)學(xué)習(xí)筆記04-菜單導(dǎo)航的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!