单击WPF MVVM中的Button Command时,将文本框值复制到另一个

时间:2017-05-16 11:22:18

标签: c# wpf xaml mvvm

我有两个文本框,想要在我点击Button时将第一个文本框值复制到另一个文本框,这应该通过使用WPF中的命令来完成。

这是我的情景:

  1. 第一个文本框绑定Person类。
  2. 的值
  3. 按钮显示简单的MsgBox,用于验证Command是否正确执行。
  4. 那么,我想将第一个文本框值传递给第二个文本框(使用Command)?
  5. XML文件:

    <Window x:Class="PrismDemo.Views.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:vm="clr-namespace:PrismDemo.ViewModels"
            Title="MainWindow" Height="350" Width="525">
        <Window.DataContext>
            <vm:Person x:Name="vmmmm1" />
        </Window.DataContext>
        <Grid>
    
    
    <TextBox x:Name="fName" Grid.Row="1" Height="30" Width="100" Text="{Binding Path=FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    
    <Button Name="Submit" Grid.Row="2" Height="30" Width="100" Content="Submit Me" Command="{Binding submitCommand}" CommandParameter="{Binding Text, ElementName=fName}"/>
    
    <TextBox x:Name="display" Grid.Row="3" Height="30" Width="100" Text="{}" />
    

    人员类(ViewModel):

    public class Person:INotifyPropertyChanged
    {
        private string _firstName;
        private string _copyName;  
        public ICommand submitCommand {get;set;}
        public Person()
        {
            _firstName = "Ronaldo";
            submitCommand = new RelayCommand(MyMethod, canExecuteMethod);
        } 
    
        public string FirstName
        {
            get 
            { 
                return _firstName; 
            }
            set
            { 
                _firstName = value;
                OnPropertyUpdated(FirstName);
                //OnPropertyUpdated(CopyName);
            }
        }
    
        public string CopyName
        {
            get
            {
                return _copyName;
            }
            set
            {
                OnPropertyUpdated(CopyName);
            }
        }
    
        private void OnPropertyUpdated(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }       
        }
    
        private bool canExecuteMethod(object parameter)
        {
            return true;
        }
    
        private void MyMethod(object parameter)
        {
            MessageBox.Show("Welcome to Command Demo...");
            //if (parameter == null) return;
            //_copyName = parameter.ToString();           
            this._copyName = _firstName;                    
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    任何帮助将不胜感激。 谢谢!!

3 个答案:

答案 0 :(得分:0)

这里你不需要CommandParameter。

<Button Name="Submit" Grid.Row="2" Height="30" Width="100" Content="Submit Me" Command="{Binding submitCommand}" />

添加Display属性:

    public string Display
    {
        get
        {

            return _display;
        }
        set
        {
            _display = value;
            OnPropertyUpdated(Display);
        }
    }

修复第二个TextBox中的绑定:

<TextBox x:Name="display" Grid.Row="3" Height="30" Width="100" Text="{Binding Display}" />

更新MyMethod:

       private void MyMethod(object parameter)
       {
           MessageBox.Show("Welcome to Command Demo...");
           Display = FirstName;                    
       }

答案 1 :(得分:0)

以下是如何将文本从一个文本框复制到另一个文本框。

这是dataContext behinde MainWindow

 public class TestVM : INotifyPropertyChanged
    {
        public TestVM()
        {
            CopyCommand = new RelayCommand<string>(OnCopyExecuted);
        }

        private void OnCopyExecuted(string commandParameter)
        {
            TextUpdate = commandParameter;
        }

        private string _textUpdate;

        public string TextUpdate
        {
            get { return _textUpdate; }
            set
            {
                if (_textUpdate != value)
                {
                    _textUpdate = value;
                    OnPropertyChanged();
                }
            }
        }


        public RelayCommand<string> CopyCommand { get; private set; }


        public event PropertyChangedEventHandler PropertyChanged = delegate { };
        public virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }

可以采用参数的通用RelayCommand

public class RelayCommand<T> : ICommand
    {
        private Action<T> _executeMethod;
        private Func<T, bool> _canExecuteMethod;

        #region RelayCommand ctor

        public RelayCommand(Action<T> executeMethod)
        {
            _executeMethod = executeMethod;
        }

        public RelayCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
        {
            _executeMethod = executeMethod;
            _canExecuteMethod = canExecuteMethod;
        }

        #endregion

        public void RaiseCanExecuteChanged()
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }


        #region ICommand Members

        bool ICommand.CanExecute(object parameter)
        {
            var Tparam = (T)parameter;
            if (_canExecuteMethod != null)
                return _canExecuteMethod(Tparam);
            if (_executeMethod != null)
                return true;
            return false;
        }

        void ICommand.Execute(object parameter)
        {
            if (_executeMethod != null)
                _executeMethod((T)parameter);
        }

        public event EventHandler CanExecuteChanged = delegate { };

        #endregion
    }

和MainWindow xaml只是为了显示目的

<Window.DataContext>
        <local:TestVM />
    </Window.DataContext>
    <Grid>
        <TextBox x:Name="txt1"
            Height="35"
                 Width="150"
                 Margin="49,62,318,224" />
        <TextBox Text="{Binding TextUpdate}"
            Height="35"
                 Width="150"
                 Margin="313,62,54,226" />
        <Button Command="{Binding CopyCommand}"
                CommandParameter="{Binding ElementName=txt1,Path=Text}"
                Content="Copy"
                Grid.Row="0"
                Margin="208,157,198,132" />
    </Grid>

它在工作。现在,您可以根据自己的需要实施它。

答案 2 :(得分:0)

你几乎是对的....它正确地在我的位置工作,只需在你的代码中进行以下更改 只需删除命令参数...我们不需要它并绑定复制的字符串。

<TextBox Grid.Row="1" Height="30" Width="100" Text="{Binding FirstName}" />

<Button  Grid.Row="2" Height="30" Width="100" Content="Submit Me" Command="{Binding submitCommand}"/>

TextBox  Grid.Row="3" Height="30" Width="100" Text="{Binding CopyName}" />

在View模型中进行以下更改...

public class Person:INotifyPropertyChanged{
 private string _firstName;
        private string _copyName=string.Empty;

        public Person()
        {
            _firstName = "Ronaldo";
            submitCommand = new RelayCommand(MyMethod, canExecuteMethod);
        }

        public string FirstName
        {
            get
            {

                return _firstName;
            }
            set
            {
                _firstName = value;
                OnPropertyChanged("FirstName");
            }
        }
        public string CopyName
        {
            get
            {

                return _copyName;
            }
            set
            {
                if (_copyName != value)
                {
                    _copyName = value;
                    OnPropertyChanged("CopyName");
                }
            }
        }
        public ICommand submitCommand { get; set; }



        private void MyMethod(object param)
        {
            MessageBox.Show("Welcome to Command Demo...");
            CopyName = FirstName;
        }
        private bool canExecuteMethod(object parameter)
        {
            return true;
        }

 public event PropertyChangedEventHandler PropertyChanged;
 protected void OnPropertyChanged(params string[] propertyNames)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                foreach (string propertyName in propertyNames) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                handler(this, new PropertyChangedEventArgs("HasError"));
            }
        }

}