2016-11-18 5 views
0
내가 가진 새로운 WPF 응용 프로그램 (내 첫 WPF 응용 프로그램) 만드는거야

:MVVM 빛 탐색 서비스 - 변경 MainWindow를 제목 및 크기

  • 닷넷 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 
} 

당신의 MainWindow를 당신은 당신이 얻을 수 heres는 어디 이제

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

을 변경하려면 어떤 속성을 원하는 재미, 당신은 그렇게 펍 서브 이벤트를 크레테 수 있습니다

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

그리고 나서 당신이 원하는 때 pu blishe 단순히

Title="{binding TitleProperty}" 
때문에,

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

수행 및 바인딩의 창 내용을 변경