使用MVVM在WPF绑定命令 [英] Bind command in WPF using MVVM

查看:366
本文介绍了使用MVVM在WPF绑定命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习 MVVM WPF 。我有一个 XAML 文件在我的项目,并已在后面的code一个简单的单击事件处理程序。

I am learning MVVM and WPF. I have a xaml file in my project and which has a simple click event handler in the code behind.

现在我想要做同样的 MVVM 。我看了很多文章,也看了很多答案的特种部队。但还是无法做到这一点。

Now I want to do the same in MVVM. I read a lot of articles and also read many answers in sof. But still unable to do this.

任何人都可以请举个简单的例子,其中一个按钮点击事件中完成的 MVVM

Can anyone please give a simple example in which a button click event is done in MVVM.

修改

<Window x:Class="WhiteBalance.BaseCalWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:uc="clr-namespace:NumberUpDownControl;assembly=NumberUpDownControl"
        xmlns:viewn="clr-namespace:WhiteBalance.ViewModels"
        Title="RefImgSettingWindow"  Height="900" Width="1000" ResizeMode="NoResize" 
        BorderThickness="4">
    <Window.Resources>
        <viewn:DashBoardViewModel x:Key="demokey"></viewn:DashBoardViewModel>
    </Window.Resources>
    <Grid x:Name="gdParent" DataContext="{StaticResource demokey}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="490" />
            <ColumnDefinition Width="488*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="300" />
            <RowDefinition Height="300" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Grid.Column="0">
            <Label Content="{Binding Path=NAME,Mode=TwoWay}" Height="28" Name="lblTest" />
            <Button Content="Capture" Height="23" Name="btnCapture" Width="75" Command="{Binding Path=SaveCommand}"
                             Canvas.Left="94" Canvas.Top="254" />

        </StackPanel>
    </Grid>
</Window>

namespace WhiteBalance.ViewModels
{
    public class DashBoardViewModel: ObservableObject
    {
        private string _name = "dsqdasd";

        public string NAME
        {
            get { return _name; }
            set { _name = value; }
        }

        public ICommand SaveCommand
        {
            get;
            set;
        }

        private bool CanExecuteSaveCommand()
        {
            return true;    // !string.IsNullOrEmpty(LastName);
        }

        private void CreateSaveCommand()
        {
            SaveCommand = new RelayCommand(SaveExecute, CanExecuteSaveCommand);
        }

        public void SaveExecute()
        {
            //Person.Save(_newPerson);
            NAME = "Changed Name";
        }

        public DashBoardViewModel()
        {
            //objModel.TestText = "This will change";
            NAME = "TestName";
        }
    }
}

在此先感谢。

Thanks in advance.

推荐答案

您可以将绑定在<一个href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.command%28v=vs.100%29.aspx">Command按钮来返回<一个任何财产属性href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand%28v=vs.100%29.aspx">ICommand. 棱镜实现所谓的 DelegateCommand ,这是非常容易使用(这里是敲除关闭吧):

You can bind the Command property of the button to any property that returns ICommand. Prism implements a nice convenient command called DelegateCommand that is very easy to use (here is a knock-off of it):

public ICommand MyButtonClickCommand 
{
    get { return new DelegateCommand<object>(FuncToCall, FuncToEvaluate); }
}

private void FuncToCall(object context)
{
    //this is called when the button is clicked
}

private bool FuncToEvaluate(object context)
{
    //this is called to evaluate whether FuncToCall can be called
    //for example you can return true or false based on some validation logic
    return true;
}



<Button x:Name="myButton" Command="{Binding MyButtonClickCommand}" />

在$ C $的CProject例如如何使用命令在WPF 有一个非常相似的示例与code,你可以很容易地工作,通过。在previous堆栈溢出问题有使用RoutedCommands一个例子,它是静态绑定:的 如何关闭命令绑定到一个按钮和<一href="http://stackoverflow.com/questions/12422945/how-to-bind-wpf-button-to-a-command-in-viewmodelbase">How到WPF按钮绑定到ViewModelBase命令?的有一个稍微更高级的例子。

The CodeProject example How to use Commands in WPF has a very similar example with code that you can easily work through. The previous Stack Overflow question has an example using RoutedCommands that are statically bound to: How to bind Close command to a button, and How to bind WPF button to a command in ViewModelBase? has a slightly more advanced example.

这篇关于使用MVVM在WPF绑定命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆