多表格绑定数据

我的项目有两个WPF表单:Form1和Form2。 在Form1中,我有1个按钮来调用Form2,textBox1,textBox2,textBox3,textBox4,Form2只有一个textBox和一个Save按钮。 所以当我点击按钮时,它会显示Form2。 在textBox中我创建了一个模板文本,如:

"blablabla %txt1% blablabla %txt2% blabla %txt3% blabla" 

我单击“保存”按钮以保存它。 返回Form1时,textBox4将显示模板文本中的内容,其中%txt1%,%txt2%,%txt3%将更改取决于textBox1,textBox2,textBox3。 我打算使用MultiBinding将textBox1,2,3中的内容绑定到textBox4中,就像这样:

   <MultiBinding StringFormat = "blablabla {0} blablabla {1} blabla {2} blabla"       

而我的问题是:如何获得

 "blablabla {0} blablabla {1} blabla {2} blabla" 

从Form2中的textBox并将其放到StringFormat?

这是完整的代码如何从表单2获取值并使用转换器以表单1显示结果

  1. 在表单2中,从文本框中获取值

    //打开表单2并从文本框中获取值

     private void button1_Click(object sender, RoutedEventArgs e) { var form2 = new Form2 {Owner = this}; form2.ShowDialog(); if(form2.DialogResult==true) { this.formatTemplate.Text = form2.DataContext as string; } } 

在表单2中设置关闭按钮并将文本框值发送到表单1

 private void btnClose_Click(object sender, RoutedEventArgs e) { this.DataContext = textBox1.Text; this.DialogResult = true; } 

在forms1的XAML中

                     

和转换器代码:

 public class Converter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { var formatsource = values[3] as string; // text value in textboxt formatTemplate var re = new Regex(@"%[A-Za-z0-9]+%"); //match any text surrounded by % sign var count = 0; foreach (var m in re.Matches(formatsource)) { formatsource= re.Replace(formatsource, values[count++] as string, 1); // replace one match at the time } return formatsource; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }