user2627651
user2627651

Reputation: 63

Multi-Forms Binding data

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"

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>

And my problem: how to get

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

from textBox in Form2 and put it to StringFormat?

Upvotes: 3

Views: 308

Answers (1)

Inga
Inga

Reputation: 482

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

  1. in form 2 and get the value from textbox

    //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;
    
        }
    }
    

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

in the XAML of form 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>

and Converter code:

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

Upvotes: 0

Related Questions