首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多表单绑定数据

多表单绑定数据
EN

Stack Overflow用户
提问于 2013-08-08 23:20:40
回答 1查看 237关注 0票数 3

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

代码语言:javascript
复制
"blablabla %txt1% blablabla %txt2% blabla %txt3% blabla"

我点击保存按钮来保存它。返回Form1时,textBox4将显示模板文本中的内容,其中%txt1%、%txt2%、%txt3%将根据textBox1、textBox2、textBox3的不同而变化。我打算使用MultiBinding将textBox4 1、2、3中的内容绑定到textBox4中,如下所示:

代码语言:javascript
复制
<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>

我的问题是:如何获得

代码语言:javascript
复制
"blablabla {0} blablabla {1} blabla {2} blabla"

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

EN

回答 1

Stack Overflow用户

发布于 2013-08-12 09:25:40

这是如何从Form2获取值并使用转换器在Form1中显示结果的完整代码

在表单2中使用

  1. 并从textbox中获取值

//打开Form2并从textbox中获取值

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

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

代码语言:javascript
复制
private void btnClose_Click(object sender, RoutedEventArgs e)
    {
        this.DataContext = textBox1.Text;
        this.DialogResult = true;
    }

在表单1的XAML中

代码语言:javascript
复制
<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>

和Converter代码:

代码语言:javascript
复制
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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18129879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档