2016-11-18 7 views
0

私は(私の最初のWPFアプリケーション)を使用して新しいWPFアプリケーションを作成しています:MVVMライトナビゲーションサービス - 変更メインウィンドウのタイトルとサイズ

  • .NET 4.0
  • MVVMライト
  • C#
  • をMahApps Metro

私はすでにMVVM Lightのナビゲーションサービスを使用してナビゲーションを実装しています。私は同じ目的を達成するためにMainWindowとPagesを使用しています。

<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ページです:私のMainWindow.xamlで

私は私の現在のビューを変更しているメインフレームを持って

<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> 

質問:

  • 現在のビューに基づいてウィンドウのタイトルとサイズを変更するにはどうすればよいですか?出来ますか?

答えて

0

まずは、MainWindowのバインディング、タイトルなどがハードコードされた値であることを理解しておく必要があります。あなたのメインウィンドウで

今私が見つけることができる最も簡単な方法は、モデルを持つことで、たとえば

public class ViewPayload{ 
    public string Title{get;set;} 
    //more properties here 
} 

あなたはその後、今それを取得HERESにどこに

public class MainWindow : MetroWindow{ 
public string Title{get;set;} 
// more properties here 
} 

を変更したいのプロパティをしたいです楽しい、あなたのようなパブのサブイベントをcretすることができます

private readonly IEventAggregator _eventAggregator = new EventAggregator(); 
_eventAggregator.GetEvent<ViewPayload>().Subscribe(ChangePropertiesFromModel); 

あなたがしたいときblishe単に

Title="{binding TitleProperty}" 
ので、

_eventAggregator.GetEvent<ViewPayload>().Publish(PublisPayloadMethod()); 

、やると結合にウィンドウの内容を変更します

関連する問題