[英]Determining out the correct property return type for a data binding when using MVVM Light in a C#/WPF app?
[英]C# binding won't work in WPF when using navigation in MVVM Light
我使用MVVMLight和MahApps創建Flat UI應用程序(WPF)。 我正在嘗試使導航正常工作。
一切似乎都還可以,但是在主窗口中由Frame托管的Page上的數據綁定存在問題。 我聲明要在登錄視圖中綁定LoginViewModel,但是當我嘗試單擊一個按鈕(連接到viewmodel中的RelayCommand)時,什么都沒有發生(我已經用調試器檢查了它)。
如何將頁面綁定到viewmodel的好方法?
這是我的Login.xaml(頁面):
<Page x:Class="Project.View.Login"
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:local="clr-namespace:Project"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
mc:Ignorable="d"
d:DesignHeight="480" d:DesignWidth="640"
Title="Login"
DataContext="{Binding LoginViewModel, Source={StaticResource Locator}}">
<Grid>
<Button Command="{Binding GoToVote}" Content="{Binding Path=ButtonText}" HorizontalAlignment="Left" Margin="224,220,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="224,320,0,0" TextWrapping="Wrap" Text="{Binding Path=ButtonText, Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>
</Grid>
</Page>
MainWindow.xaml(窗口):
<Controls:MetroWindow x:Class="Project.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:Project"
mc:Ignorable="d"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
DataContext="{Binding MainViewModel, Source={StaticResource Locator}}"
Title="MainWindow" Height="480" Width="640">
<Frame Source="View/Login.xaml" x:Name="MainFrame" NavigationUIVisibility="Hidden" />
</Controls:MetroWindow>
ViewModelLocator.cs:
using Project.Tools;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
using System;
namespace Project.ViewModel
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// </summary>
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
////if (ViewModelBase.IsInDesignModeStatic)
////{
//// // Create design time view services and models
//// SimpleIoc.Default.Register<IDataService, DesignDataService>();
////}
////else
////{
//// // Create run time view services and models
//// SimpleIoc.Default.Register<IDataService, DataService>();
////}
SimpleIoc.Default.Register<MainViewModel>();
SimpleIoc.Default.Register<LoginViewModel>();
SimpleIoc.Default.Register<VoteViewModel>();
var navigationService = new FrameNavigationService();
navigationService.Configure("Login", new Uri("../View/Login.xaml", UriKind.Relative));
navigationService.Configure("Vote", new Uri("../View/Vote.xaml", UriKind.Relative));
SimpleIoc.Default.Register<IFrameNavigationService>(() => navigationService);
}
public MainViewModel MainViewModel
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public LoginViewModel LoginViewModel
{
get
{
return ServiceLocator.Current.GetInstance<LoginViewModel>();
}
}
public VoteViewModel VoteViewModel
{
get
{
return ServiceLocator.Current.GetInstance<VoteViewModel>();
}
}
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
}
LoginViewModel.cs:
using Project.Tools;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project.ViewModel
{
public class LoginViewModel : ViewModelNavigationBase
{
private string _buttonText;
public RelayCommand GoCommand { get; private set; }
void GoToVote()
{
_navigationService.NavigateTo("Vote");
}
public LoginViewModel(IFrameNavigationService navigationService) : base(navigationService)
{
GoCommand = new RelayCommand(GoToVote);
}
public string ButtonText
{
get { return _buttonText; }
set
{
_buttonText = value;
}
}
}
}
您是否嘗試過在第一個<Grid>
上設置數據綁定? 在根級別的對象上設置它可能會引起問題,因為該屬性可能是由承載頁面/窗口的控件設置的。
<Page x:Class="Project.View.Login"
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:local="clr-namespace:Project"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
mc:Ignorable="d"
d:DesignHeight="480" d:DesignWidth="640"
Title="Login">
<Grid DataContext="{Binding LoginViewModel, Source={StaticResource Locator}}">
<Button Command="{Binding GoToVote}" Content="{Binding Path=ButtonText}" HorizontalAlignment="Left" Margin="224,220,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="224,320,0,0" TextWrapping="Wrap" Text="{Binding Path=ButtonText, Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>
</Grid>
</Page>
在主窗口上可能是同一件事。
(我猜您在App.xaml
聲明了Locator
。)
嘗試更改)
使用GalaSoft.MvvmLight.Command; ->使用GalaSoft.MvvmLight.CommandWpf;
好,解決了
我犯了兩個可怕的錯誤(我不知道怎么做)。 首先:在我的類ViewModelNavigationBase中,我忘記了實現ViewModelBase (它是導航對象的擴展)。
第二-我將按鈕命令綁定到功能,而不是屬性( ICommand )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.