本文介绍了多表单绑定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有两个WPF窗体:Form 1和Form。在Form1我有1按钮调窗体2,textBox1的,TextBox2中,textBox3,textBox4,窗体2只有一个文本框和一个保存按钮。所以,当我点击按钮,它显示窗体2。 TextBox中我做一个模板文本,如:

My project have two WPF Forms: Form1 and Form2. In Form1 I have 1 button to call Form2, textBox1, textBox2, textBox3, textBox4, Form2 has only one textBox and a Save button. So when I click button, it show Form2. In textBox I make a template text like:

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

我点击Save按钮保存。当返回Form1上,textBox4将显示模板文本中,%TXT1%,%TXT2%,%txt3%会发生变化的内容取决于textBox1中,TextBox2中,textBox3。我打算用MultiBinding在textBox1,2,3绑定内容纳入textBox4,它这样:

I click Save button to Save it. When return Form1, textBox4 will display content in template text in which %txt1%, %txt2%,%txt3% will change depend on textBox1, textBox2, textBox3. I intend to use MultiBinding to bind content in textBox1,2,3 into textBox4, it like that:

<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的文本框,把它的StringFormat?

from textBox in Form2 and put it to StringFormat?

推荐答案

这是完整的代码如何从表2获得价值并使用转换器显示导致表1

This is complete code how to get value from form 2 and use converter to display result in form 1


  1. 在表格2,并从文本框中的值

  1. in form 2 and get the value from textbox

//打开的窗体2,并从文本框中的值

//open form 2 and get the value from textbox

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

in the form 2 set close button and send textbox value to form 1

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

在表格1

<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();
    }
}

这篇关于多表单绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:26