簡體   English   中英

單擊WPF MVVM中的Button Command時,將文本框值復制到另一個

[英]copy textbox value to another when click on Button Command in WPF MVVM

我有兩個文本框,並且每當我點擊Button時想要將第一個文本框值復制到另一個文本框,這應該通過使用WPF中的命令來完成。

這是我的情景:

  1. 第一個文本框綁定Person類中的值。
  2. Button顯示簡單的MsgBox ,用於驗證Command是否正確執行。
  3. 那么,我想將第一個文本框值傳遞給第二個文本框(使用Command)?

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

Person類(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;
}

任何幫助將不勝感激。 謝謝 !!

這里不需要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;                    
       }

以下是將文本從一個文本框復制到另一個文本框的方法。

這是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));
        }

    }

可以接受參數的Generic 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>

它正在發揮作用。 現在您可以根據自己的需要實施它。

你差不多......它正確地在我的地方工作,只需在你的代碼中進行以下更改只需刪除命令參數...我們不需要它並綁定復制的字符串。

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM