C#绑定赢得';在MVVM灯光下使用导航时,不能在WPF中工作

C#绑定赢得';在MVVM灯光下使用导航时,不能在WPF中工作,c#,.net,wpf,xaml,mvvm,C#,.net,Wpf,Xaml,Mvvm,我使用MVVMLight和MahApps创建平面UI应用程序(WPF)。我想让导航系统正常工作 看起来一切正常,但主窗口上框架托管的页面上的数据绑定存在问题。我声明我想在Login视图中绑定LoginViewModel,但当我尝试单击一个按钮(它连接到viewmodel中的RelayCommand)时,什么也没发生(我已经用调试器检查过) 如何将页面绑定到viewmodel的好方法 这是我的Login.xaml(第页): 您是否尝试过在第一个上设置数据绑定?在根级别对象上设置该属性可能会导致问

我使用MVVMLight和MahApps创建平面UI应用程序(WPF)。我想让导航系统正常工作

看起来一切正常,但主窗口上框架托管的页面上的数据绑定存在问题。我声明我想在Login视图中绑定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">

    <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

它没有按预期工作。奇怪的事情发生了-当我点击按钮时,并没有任何动作,只有刷新按钮的内容(按钮的内容应该在输入文本框后刷新-但它不会工作)。你们有snoop吗?这将告诉您数据上下文实际上是什么。一旦你知道了这一点,你就会对正在发生的事情有一个更好的了解。(注意snoop正在嗅探什么!)这是一个有用的工具。谢谢
<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>
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
        }
    }
}
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;
            }
        }
    }
}
<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>