MVVM Light导航服务-更改MainWindow标题和大小 [英] MVVM Light Navigation Service - Change MainWindow Title and Size
问题描述
我正在使用(我的第一个WPF应用程序)创建一个新的WPF应用程序:
I'm creating a new WPF application with (my first WPF app):
- .Net 4.0
- MVVM灯
- C#
- MahApps Metro
我已经使用MVVM Light中的导航服务"实现了导航.我正在使用MainWindow和Pages来完成相同的任务.
I've already implemented navigation using the Navigation Service in MVVM Light. I'm using a MainWindow and Pages in order to accomplish the same.
在MainWindow.xaml中,有一个MainFrame正在更改当前视图:
In my MainWindow.xaml I have a MainFrame that is changing my current View:
<Controls:MetroWindow x:Class="App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:resx="clr-namespace:MaverickDesktop.Resources"
Title="My title"
Height="280"
Width="500">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Frame Source="\Views\LoginView.xaml" NavigationUIVisibility="Hidden" Name="MainFrame"></Frame>
</Controls:MetroWindow>
这是我的LoginView.xaml页面:
This is my LoginView.xaml Page:
<Page x:Class="App.Views.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
Dialog:DialogParticipation.Register="{Binding}"
mc:Ignorable="d"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:resx="clr-namespace:MaverickDesktop.Resources"
Height="500"
Width="700"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
DataContext="{Binding Main, Source={StaticResource Locator}}"
>
<Grid Margin="0,0,0,10">
<Label Content="User:" HorizontalAlignment="Left" Margin="24,37,0,0" VerticalAlignment="Top" FontSize="24"/>
<Label Content="Password:" HorizontalAlignment="Left" Margin="24,96,0,0" VerticalAlignment="Top" FontSize="24"/>
<TextBox HorizontalAlignment="Left" Height="42" Margin="172,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="274" Name="txtUser" FontSize="24" Controls:TextBoxHelper.ClearTextButton="True" Text="{Binding personnel.personnel_key, Mode=TwoWay}"/>
<PasswordBox HorizontalAlignment="Left" Height="42" Margin="172,96,0,0" VerticalAlignment="Top" Width="274" Name="txtPassword" FontSize="24" Controls:TextBoxHelper.ClearTextButton="True" PasswordChar="*" PasswordChanged="txtPassword_PasswordChanged"/>
<Button Content="Ingresar" HorizontalAlignment="Left" IsDefault="True" Margin="172,169,0,-22" VerticalAlignment="Top" Width="274" Height="44" FontSize="24" Name="btnLogin" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding btn_login_click}"/>
</Grid>
</Page>
问题:
- 如何根据当前视图更改窗口的标题和大小?有可能吗?
推荐答案
首先,您必须了解MainWindow中的绑定,标题等概念都是硬编码的值.
Well first, you have to undersand the idea of bindings, your title,etc in MainWindow are hardcoded values.
现在我能找到的最简单的方法是拥有一个模型,例如
Now the simplest way i can find is to have a model, example
public class ViewPayload{
public string Title{get;set;}
//more properties here
}
然后在您的MainWindow上,需要要更改的属性
On your MainWindow you then want properties for what you want to change
public class MainWindow : MetroWindow{
public string Title{get;set;}
// more properties here
}
现在这里很有趣,您可以像这样创建pub子事件
Now heres where it gets fun, you can crete a pub sub event like so
private readonly IEventAggregator _eventAggregator = new EventAggregator();
_eventAggregator.GetEvent<ViewPayload>().Subscribe(ChangePropertiesFromModel);
然后,当您只想发布时,
And then when you want to publishe simply do,
_eventAggregator.GetEvent<ViewPayload>().Publish(PublisPayloadMethod());
并将窗口内容更改为绑定,因此
and change the window content to binding, so
Title="{binding TitleProperty}"
这篇关于MVVM Light导航服务-更改MainWindow标题和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!