2014-09-19 69 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

相關問題