在Wpf MVVMLight中将按钮绑定到RelayCommand

时间:2018-08-07 11:16:50

标签: c# wpf mvvm mvvm-light relaycommand

您好,我正在Wpf MVVM项目上工作,但是我无法弄清楚如何将Xaml中具有Command属性的按钮绑定到Viewmodel中的RelayCommand,我在网上找到了多个答案,但我不明白(实现ICommand接口并可以执行和执行其他操作。),问题是我已经创建了一个由其他开发人员创建的项目,他们只是简单地将按钮绑定到Xaml中:

将完整代码显示在视图中:

<Window x:Class="MvvmLight3.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:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:mvvm="http://www.galasoft.ch/mvvmlight"
    xmlns:ignore="http://www.galasoft.ch/ignore"
    mc:Ignorable="d ignore"
    Height="300"
    Width="300"
    Title="MVVM Light Application"
    DataContext="{Binding Main, Source={StaticResource Locator}}">

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Skins/MainSkin.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<Grid x:Name="LayoutRoot">

    <TextBlock FontSize="36"
               FontWeight="Bold"
               Foreground="Purple"
               VerticalAlignment="Center"
               HorizontalAlignment="Center"
               TextWrapping="Wrap" ><Run Text="WelcomeTitle"/><InlineUIContainer>
            <Label Content="{Binding welcome}"/>
        </InlineUIContainer></TextBlock>
    <Button Content="Button" Command="{Binding The_Command}" HorizontalAlignment="Left" Margin="170,80,0,0" VerticalAlignment="Top" Width="75">

    </Button>
</Grid>

,在ViewModel中,完整代码为:

    using GalaSoft.MvvmLight;
    using GalaSoft.MvvmLight.CommandWpf;
    using MvvmLight3.Model;
    using System.Windows.Input;

namespace MvvmLight3.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.mvvmlight.net
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;

        /// <summary>
        /// The <see cref="WelcomeTitle" /> property's name.
        /// </summary>
        public const string WelcomeTitlePropertyName = "WelcomeTitle";

        private string _welcomeTitle = string.Empty;
        private string _welcome = "this work";
        /// <summary>
        /// Gets the WelcomeTitle property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string WelcomeTitle
        {
            get
            {
                return _welcomeTitle;
            }
            set
            {
                Set(ref _welcomeTitle, value);
            }
        }
        public string welcome
        {
            get
            {
                return _welcome;
            }
            set
            {
                Set(ref _welcome, value);
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;
            _dataService.GetData(
                (item, error) =>
                {
                    if (error != null)
                    {
                        // Report error here
                        return;
                    }

                    WelcomeTitle = item.Title;
                });
        }
        private RelayCommand _The_Command;
        public RelayCommand The_Command
        {
            get
            {
                return _The_Command
                    ?? (_The_Command = new RelayCommand(
                    () =>
                    {
                        //some Code
                    }));
            }
        }
        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}

在我的情况下,执行不输入RelayCommand。

ViewModelLocator.cs中的代码

    /*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:MvvmLight3.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>

  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
*/

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
using MvvmLight3.Model;

namespace MvvmLight3.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// See http://www.mvvmlight.net
    /// </para>
    /// </summary>
    public class ViewModelLocator
    {
        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)
            {
                SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
            }
            else
            {
                SimpleIoc.Default.Register<IDataService, DataService>();
            }

            SimpleIoc.Default.Register<MainViewModel>();
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
        }
    }
}

该项目是MVVMLight(wpf451)模板。 谢谢。

1 个答案:

答案 0 :(得分:0)

它起作用了,这有点愚蠢,实际上,如果您注意到我的中继命令(The_Command)为空,并且尽管其中包含注释(//一些代码),则当我单击时,get或return的断点永远不会通过按钮。

解决方案: 在命令内添加了功能代码后,它起作用了:例如添加了一个简单的MessageBox.Show。因此绑定是正确的。对于信息,我正在使用VisualStudio Enterprise 2017。