在将ViewModelLocator的ApplicationViewModel绑定到MainWindow.xaml以更改应用程序中的页面时,我陷入了困境。
绑定正在工作,显示第一页(LoginPage),但是当我尝试更改它们时,它没有任何作用。看来PropertyChanged事件不起作用。
我的应用程序代码: MainWindow.xaml:
<Window x:Class="DebtDiary.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DebtDiary"
xmlns:localization="clr-namespace:DebtDiary.Localization"
xmlns:core="clr-namespace:DebtDiary.Core;assembly=DebtDiary.Core"
mc:Ignorable="d" WindowStyle="None"
AllowsTransparency="True"
WindowStartupLocation="CenterScreen"
Icon="../Images/Logo/DebtDiaryIcon.ico"
Title="{x:Static localization:Strings.ApplicationName}"
MinWidth="{Binding MinimumWidth}"
MinHeight="{Binding MinimumHeight}"
Width="{Binding MinimumWidth, FallbackValue=1280}"
Height="{Binding MinimumHeight, FallbackValue=720}">
<Window.Resources>
<Style TargetType="{x:Type local:MainWindow}">
<!-- Window Chrome Settings -->
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome ResizeBorderThickness="{Binding ResizeBorderThickness}" CaptionHeight="{Binding TitleBarHeight}"/>
</Setter.Value>
</Setter>
<!-- New window template -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<!-- Outer border -->
<Border Background="Gray">
<Grid>
<Grid.RowDefinitions>
<!-- Title bar -->
<RowDefinition Height="{Binding TitleBarHeight, FallbackValue=35}"/>
<!-- Window Content -->
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Title bar -->
<Grid Grid.Row="0" Background="{StaticResource DarkGreyBrush}">
<Grid.ColumnDefinitions>
<!-- Icon -->
<ColumnDefinition Width="Auto"/>
<!-- Window title -->
<ColumnDefinition Width="*"/>
<!-- Minimize button -->
<ColumnDefinition Width="Auto"/>
<!-- Maximize / Restore button -->
<ColumnDefinition Width="Auto"/>
<!-- Exit button -->
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!-- Icon -->
<Image Grid.Column="0" Source="../Images/Logo/DebtDiarySmallIcon.ico" Margin="3 3 3 4"/>
<!-- Title -->
<TextBlock Grid.Column="1" Margin="2 9 2 0" Style="{StaticResource TitleBarStyle}"
Text="{Binding Title, RelativeSource={RelativeSource Mode=TemplatedParent}, FallbackValue='Debt diary'}" />
<!-- Minimize button -->
<Button Grid.Column="2" Style="{StaticResource WindowControlButton}" Content="{StaticResource MinimizeIcon}"
Command="{Binding MinimizeCommand}" />
<!-- Maximize / Restore button -->
<Button Grid.Column="3" Style="{StaticResource WindowControlButton}"
Content="{TemplateBinding WindowState, Converter={local:WindowStateToIconValueConverter}}"
Command="{Binding MaximizeCommand}"/>
<!-- Close button -->
<Button Grid.Column="4" Style="{StaticResource WindowCloseButton}" Content="{StaticResource CloseIcon}"
Command="{Binding CloseCommand}"/>
</Grid>
<!-- Page content -->
<Border Grid.Row="1">
<ContentPresenter Content="{TemplateBinding Content}"/>
</Border>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<!-- Window content -->
<Grid DataContext="{Binding ApplicationViewModel, Source={x:Static local:ViewModelLocator.Instance}}">
<!-- Window background -->
<Grid.Background>
<ImageBrush ImageSource="../Images/StartScreenBackground.png" Stretch="UniformToFill" />
</Grid.Background>
<!-- Current Page -->
<Frame Content="{Binding CurrentPage, Converter={local:ApplicationPageValueConverter}}" NavigationUIVisibility="Hidden"/>
</Grid>
ViewModelLocator.cs:
public class ViewModelLocator
{
/// <summary>
/// Singleton instance of the ViewModelLocator
/// </summary>
public static ViewModelLocator Instance { get; private set; } = new ViewModelLocator();
/// <summary>
/// ApplicationViewModel Property
/// </summary>
public ApplicationViewModel ApplicationViewModel
{
get
{
IKernel kernel = new StandardKernel();
return kernel.Get<ApplicationViewModel>();
}
}
}
ApplicationViewModel.cs
public class ApplicationViewModel : BaseViewModel
{
#region Public Properties
/// <summary>
/// CurrentPage in the application
/// </summary>
public ApplicationPage CurrentPage { get; set; } = ApplicationPage.LoginPage;
#endregion
}
您是否知道如何解决?