【问题标题】:copy textbox value to another when click on Button Command in WPF MVVM单击WPF MVVM中的按钮命令时将文本框值复制到另一个
【发布时间】:2017-05-16 11:22:17
【问题描述】:

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

这是我的场景:

  1. 第一个文本框绑定来自Person 类的值。
  2. 按钮显示简单的MsgBox,验证命令正确执行。
  3. 那么,我想将第一个文本框的值传递给第二个文本框(使用命令)?

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

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

【问题讨论】:

  • 请read 告知您需要多长时间才能得到答案。
  • 欢迎来到 Stack Overflow。我已对您的文本和代码进行了格式化,以便于阅读。
  • 感谢您的信息!!!

标签: c# wpf xaml mvvm


【解决方案1】:

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

<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}" />

在视图模型中进行以下更改...

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"));
            }
        }

}

【讨论】:

  • 这对丽娜有效。很有帮助。我错过了 Property :OnPropertyChanged(CopyName); 的引号。 ,谢谢!!!
【解决方案2】:

这里不需要CommandParameter。

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

添加显示属性:

    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}" />

更新我的方法:

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

【讨论】:

    【解决方案3】:

    这里是如何将文本从一个文本框复制到另一个文本框。

    这是 MainWindow 后面的 dataContext

     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>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-07
      • 2014-01-01
      • 2013-09-08
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      相关资源
      最近更新 更多