2014-09-19 64 views
0

我试图从一个文本框发送文本到另一个文本框,如果复选框被选中。这些字段是在两个不同的XAML文件中创建的。如果复选框被选中,将文本从一个字段复制到另一个字段

第一个XAML文件(InvolvedPersonStackPanel)包含复选框和文本框:

enter image description here

如果此复选框被选中,从所涉人员文本框中的文本应该被复制到另一个文本在不同的XAML文件中创建对话框,如下图所示:

enter image description here

在InvolvedPersonStackPanel.xaml.cs文件我写了这个代码:

void LivesWithCheckBox_Checked(object sender, RoutedEventArgs e) 
{ 
    per.LivesWithTextBox.Text = personTextBox.Text; 
} 

这不适合我,但我不确定这个问题。也许我应该尝试在另一个xaml.cs文件中。

+0

如果有人改变了所涉人员会发生什么在检查复选框后。它们应该同步吗?应该可以在不更改InvolvedPerson的情况下更改LivesWithTextBox文本吗? – 2014-09-19 22:25:01

+0

只要该复选框被选中,它们应该同步。 – 2014-09-19 22:29:47

回答

1

我建议你使用ViewModel在两个视图之间进行绑定,就像这样。请阅读MVVM模式了解更多信息。

基本上,您的每个XAML文件将是一个View。然后您应该确保它们与它们的DataContext共享ViewModel的相同实例。如果这样做,你可以使用下面的ViewModel

我强烈要求任何人想与WPF一起工作,以便尽早学习MVVM。

public class MyViewModel : INotifyPropertyChanged 
{ 
    private string _involvedPerson; 
    private string _livesWithPerson; 
    private bool _livesWith; 

    public string InvolvedPerson 
    { 
     get { return _involvedPerson; } 
     set 
     { 
      _involvedPerson = value; 
      OnPropertyChanged("InvolvedPerson"); 
     } 
    } 

    public string LivesWithPerson 
    { 
     get 
     { 
      if (LivesWith) 
      { 
       return InvolvedPerson; 
      } 
      return _livesWithPerson; 
     } 
     set 
     { 
      if (LivesWith) 
      { 
       InvolvedPerson = value; 
      } 
      else 
      { 
       _livesWithPerson = value; 
      } 
      OnPropertyChanged("LivesWithPerson"); 
     } 
    } 
    public bool LivesWith 
    { 
     get { return _livesWith; } 
     set 
     { 
      _livesWith = value; 
      if (_livesWith) 
      { 
       LivesWithPerson = null; 
      } 
      OnPropertyChanged("LivesWith"); 
     } 
    } 

    private void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

请不说,还可以让你的检查事件改变对LivesWith文本框的结合,使其指向所涉人员属性。但个人而言,我更喜欢在ViewModel中使用这样的逻辑,通过单元测试可以很容易地进行测试。

+1

帮我一把。这看起来像OP向我发布的内容。 – Paparazzi 2014-09-19 22:26:40

+0

只是为了避免混淆其他人。我现在也根据OP对我的问题的回应编辑了我的答案。 – 2014-09-19 22:40:34

+0

请解释downvote? – 2014-09-19 22:50:18

0

继承人我的回答,先生,你的逻辑是正确的,但你需要这个:

C#WPF

设计:

2文本 1复选框

//您之前的回答

private void YourCheckBox_Checked(object sender, RoutedEventArgs e) 
    { 
     txtOutput.Text = txtInput.Text; 
    } 

// ----------你错过了这一个!!!!您需要添加此

private void txtInput.Text_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     if (YourCheckBox.IsChecked == true) { txtOutput.Text = txtInput.Text; } 
    } 

//终于在这里它会看起来像:

private void YourCheckBox_Checked(object sender, RoutedEventArgs e) 
    { 
     txtOutput.Text = txtInput.Text; 
    } 

    private void txtInput.Text_TextChanged(object sender, TextChangedEventArgs e) 
    { 

     if (YourCheckBox.IsChecked == true) { txtOutput.Text = txtInput.Text; } 
    } 

//我希望它的工作原理:d

+0

这可以在同一窗体的两个字段上工作,但在将输入从一个窗体转移到另一个窗体时不起作用。 – 2014-09-22 22:05:31

相关问题