【问题标题】:Binding button to RelayCommand in Wpf MVVMLight在 Wpf MVVMLight 中将按钮绑定到 RelayCommand
【发布时间】:2018-08-07 11:16:49
【问题描述】:

嗨,我正在处理 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) 模板。 谢谢。

【问题讨论】:

  • 删除, () =&gt; (SelectedPanel != null)会发生什么
  • 这是一个例子,它不会改变任何东西,如果 selectedPanel 不为空,它会启用按钮,否则按钮会被禁用。
  • 在您的 xaml 顶部,您是否添加了数据上下文? IE。 DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
  • yes , : DataContext="{Binding Main, Source={StaticResource Locator}}" 也链接在 ViewModelLocator 中。
  • 这个按钮不在某种模板中,是吗?

标签: c# wpf mvvm mvvm-light relaycommand


【解决方案1】:

它起作用了,这有点傻,实际上如果你注意到我的中继命令(The_Command)是空的,虽然它里面有一个注释(//一些代码),但当我点击时,get 或 return 上的断点从未通过按钮。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多