【发布时间】:2017-05-16 11:22:17
【问题描述】:
我有两个文本框,并且想在单击按钮时将第一个文本框值复制到另一个文本框,这应该通过使用 WPF 中的命令来完成。
这是我的场景:
- 第一个文本框绑定来自
Person类的值。 - 按钮显示简单的
MsgBox,验证命令正确执行。 - 那么,我想将第一个文本框的值传递给第二个文本框(使用命令)?
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。我已对您的文本和代码进行了格式化,以便于阅读。
-
感谢您的信息!!!