C# 在WPF MVVM中单击按钮命令时,将文本框值复制到另一个文本框
我有两个文本框,每当我点击按钮时,我想将第一个文本框值复制到另一个文本框,这应该通过使用WPF中的命令来完成 这是我的场景:C# 在WPF MVVM中单击按钮命令时,将文本框值复制到另一个文本框,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有两个文本框,每当我点击按钮时,我想将第一个文本框值复制到另一个文本框,这应该通过使用WPF中的命令来完成 这是我的场景: <Window x:Class="PrismDemo.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<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="{}" />
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;
}
Person
类中的值MsgBox
,用于验证命令是否正确执行<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="{}" />
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}" />
修复第二个文本框中的绑定:
<TextBox x:Name="display" Grid.Row="3" Height="30" Width="100" Text="{Binding Display}" />
这里不需要CommandParameter
<Button Name="Submit" Grid.Row="2" Height="30" Width="100" Content="Submit Me" Command="{Binding submitCommand}" />
修复第二个文本框中的绑定:
<TextBox x:Name="display" Grid.Row="3" Height="30" Width="100" Text="{Binding Display}" />
下面是如何将文本从一个文本框复制到另一个文本框 这是dataContext behinde主窗口
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));
}
}
公共类TestVM:INotifyPropertyChanged
{
公共TestVM()
{
CopyCommand=新的RelayCommand(oncopycexecuted);
}
私有void OnCopyExecuted(字符串commandParameter)
{
TextUpdate=commandParameter;
}
私有字符串_textUpdate;
公共字符串文本更新
{
获取{return\u textUpdate;}
设置
{
如果(_textUpdate!=值)
{
_text更新=值;
OnPropertyChanged();
}
}
}
public RelayCommand CopyCommand{get;private set;}
公共事件PropertyChangedEventHandler PropertyChanged=委托{};
公共虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChanged(这是新的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
}
公共类RelayCommand:ICommand
{
私人行动执行方法;
私人职能执行方法;
#区域中继命令
公共中继命令(操作执行方法)
{
_executeMethod=executeMethod;
}
公共中继命令(操作执行方法、函数执行方法)
{
_executeMethod=executeMethod;
_canExecuteMethod=canExecuteMethod;
}
#端区
public void raisecancecutechanged()
{
CanExecuteChanged(此为EventArgs.Empty);
}
#区域ICommand成员
bool ICommand.CanExecute(对象参数)
{
var Tparam=(T)参数;
如果(_canExecuteMethod!=null)
返回_canExecuteMethod(Tparam);
if(_executeMethod!=null)
返回true;
返回false;
}
void ICommand.Execute(对象参数)
{
if(_executeMethod!=null)
_executeMethod((T)参数);
}
公共事件事件处理程序CanExecuteChanged=委托{};
#端区
}
和主窗口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>
它起作用了。现在,您可以根据需要实施它。以下是如何将文本从一个文本框复制到另一个文本框 这是dataContext behinde主窗口
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));
}
}
公共类TestVM:INotifyPropertyChanged
{
公共TestVM()
{
CopyCommand=新的RelayCommand(oncopycexecuted);
}
私有void OnCopyExecuted(字符串commandParameter)
{
TextUpdate=commandParameter;
}
私有字符串_textUpdate;
公共字符串文本更新
{
获取{return\u textUpdate;}
设置
{
如果(_textUpdate!=值)
{
_text更新=值;
OnPropertyChanged();
}
}
}
public RelayCommand CopyCommand{get;private set;}
公共事件PropertyChangedEventHandler PropertyChanged=委托{};
公共虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChanged(这是新的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
}
公共类RelayCommand:ICommand
{
私人行动执行方法;
私人职能执行方法;
#区域中继命令
公共中继命令(操作执行方法)
{
_executeMethod=executeMethod;
}
公共中继命令(操作执行方法、函数执行方法)
{
_executeMethod=executeMethod;
_canExecuteMethod=canExecuteMethod;
}
#端区
public void raisecancecutechanged()
{
CanExecuteChanged(此为EventArgs.Empty);
}
#区域ICommand成员
bool ICommand.CanExecute(对象参数)
{
var Tparam=(T)参数;
如果(_canExecuteMethod!=null)
返回_canExecuteMethod(Tparam);
if(_executeMethod!=null)
返回true;
返回false;
}
void ICommand.Execute(对象参数)
{
if(_executeMethod!=null)
_executeMethod((T)参数);
}
公共事件事件处理程序CanExecuteChanged=委托{};
#端区
}
和主窗口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}" />
你几乎是对的。。。。