3

我的项目有两个 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 中,如下所示:

<TextBox Name="textBox4">
 <TextBox.Text>
  <MultiBinding StringFormat = "blablabla {0} blablabla {1} blabla {2} blabla"
   <Binding ElementName = "textBox1" Path="Text"/>
   <Binding ElementName = "textBox2" Path="Text"/>
   <Binding ElementName = "textBox3" Path="Text"/>
  </MultiBinding>
 </TextBox.Text>
</TextBox>

我的问题:如何获得

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

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

4

1 回答 1

0

这是如何从表格 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;
    }

在表单 1 的 XAML 中

<Window.Resources>
        <local:Converter x:Key="converter" />
    </Window.Resources>

<Grid x:Name="LayoutRoot">
    <StackPanel>
        <TextBox Text="one" x:Name="textBox1" />
        <TextBox Text="two" x:Name="textBox2"  />
        <TextBox Text="three" x:Name="textBox3" />
        <TextBox Text="" x:Name="formatTemplate" Visibility="Collapsed" />

        <TextBox x:Name="textBox4" >
            <MultiBinding Converter="{StaticResource converter}">
                <Binding ElementName = "textBox1" Path="Text"/>
                <Binding ElementName = "textBox2" Path="Text"/>
                <Binding ElementName = "textBox3" Path="Text"/>
                <Binding ElementName="formatTemplate" Path="Text" />
            </MultiBinding>
        </TextBox>
        <Button Content="Button" Height="25" Name="button1" Width="155" Click="button1_Click" />
    </StackPanel>
</Grid>

和转换器代码:

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();
    }
}
于 2013-08-12T01:25:40.297 回答