简体   繁体   English

在MVVM Light中使用导航时,C#绑定在WPF中不起作用

[英]C# binding won't work in WPF when using navigation in MVVM Light

I use MVVMLight and MahApps to create Flat UI application (WPF). 我使用MVVMLight和MahApps创建Flat UI应用程序(WPF)。 I'm trying to get navigation working. 我正在尝试使导航正常工作。

Everything seems to be ok, but there is a problem with data binding on my Page hosted by Frame on main window. 一切似乎都还可以,但是在主窗口中由Frame托管的Page上的数据绑定存在问题。 I declared that i want to bind LoginViewModel in Login View, but when i try to click a button (which is connected to RelayCommand in viewmodel), nothing happens (I've had checked it with debugger). 我声明要在登录视图中绑定LoginViewModel,但是当我尝试单击一个按钮(连接到viewmodel中的RelayCommand)时,什么都没有发生(我已经用调试器检查了它)。

How to bind page to viewmodel good way? 如何将页面绑定到viewmodel的好方法?

MVVM Light Navigation MVVM灯光导航

Here is my Login.xaml (Page): 这是我的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 (Window): 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: 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: 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;
            }
        }
    }
}

Have you tried setting the data binding on the first <Grid> instead? 您是否尝试过在第一个<Grid>上设置数据绑定? Setting it on the root level object might be causing the problem because that property is probably being set by the control that hosts the Page / Window. 在根级别的对象上设置它可能会引起问题,因为该属性可能是由承载页面/窗口的控件设置的。

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

Probably same thing on the main window. 在主窗口上可能是同一件事。

(I'm guessing that you declare Locator in you App.xaml .) (我猜您在App.xaml声明了Locator 。)

Try to change ) 尝试更改)

using GalaSoft.MvvmLight.Command; 使用GalaSoft.MvvmLight.Command; -> using GalaSoft.MvvmLight.CommandWpf; ->使用GalaSoft.MvvmLight.CommandWpf;

Ok, solved. 好,解决了

I did two terrible mistakes (i don't know how). 我犯了两个可怕的错误(我不知道怎么做)。 First: In my class ViewModelNavigationBase i forget about implementing ViewModelBase (it's extension with navigation object). 首先:在我的类ViewModelNavigationBase中,我忘记了实现ViewModelBase (它是导航对象的扩展)。

Second - i binded button command to function, not to property ( ICommand ). 第二-我将按钮命令绑定到功能,而不是属性( ICommand )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM