1

我的项目看起来像这样,http://s23.postimg.org/mrhuocn4b/asd.png

我已经可以使用以下代码将文本框保存到 xml 文件:

private void SaveFile(object sender, RoutedEventArgs e)
    {

        SaveFileDialog saveFileDialog = new SaveFileDialog();

        saveFileDialog.DefaultExt = "xml";
        saveFileDialog.Filter = "XML Files (*.xml)|*.xml|All files (*.*)|*.*";
        saveFileDialog.FilterIndex = 1;

        if (saveFileDialog.ShowDialog() == true)
        {


            using (Stream stream = saveFileDialog.OpenFile())
            {

                StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8);
                sw.Write(GetGeneratedXML().ToString());
                sw.Close();

                stream.Close();

            } 

        }
                }


    private XElement GetGeneratedXML()
    {

        XElement userInformation = new XElement("names");
        userInformation.Add(new XElement("first", box1.Text));
       // userInformation.Add(new XElement("last", lastNameText.Text));

        return userInformation;

    }

但这是来自已经在 XAML 中创建的文本框(我仅用于测试),我想要的是保存通过单击按钮创建的所有文本框的文本。

这就是我创建文本框的方式:

XAML:

<TextBox Text="{Binding Header,UpdateSourceTrigger=PropertyChanged}"
                 BorderBrush="Black" BorderThickness="1" />
        <TextBox Text="{Binding Text,UpdateSourceTrigger=PropertyChanged}"
                 TextWrapping="Wrap"
                 VerticalScrollBarVisibility="Auto"
                 AcceptsReturn="True"
                 BorderBrush="Black" BorderThickness="1" Grid.Row="1" />

C#:

private void b_ClickEntidade(object sender, RoutedEventArgs e)
        {
            MyBox c = new MyBox();
            c.Header = "Entidade";
            c.Text = "Atributos";
            c.Margin = new Thickness(10);
            c.BorderThickness = new Thickness(1);
            LayoutRoot.Children.Add(c);
            c.MouseLeftButtonDown += Handle_MouseDownEntidade;
            c.MouseMove += Handle_MouseMoveEntidade;
            c.MouseLeftButtonUp += Handle_MouseUpEntidade;
            Canvas.SetLeft(c, 250);
            Canvas.SetTop(c, 40);
        } 

编辑 - - - - - -

这是 MyBox.cs

    public partial class MyBox : UserControl
    {
        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(MyBox),null);
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Content", typeof(string), typeof(MyBox), null);

        public string Header
        {
            get { return GetValue(HeaderProperty) as string; }
            set { SetValue(HeaderProperty, value); }
        }

        public string Text
        {
            get { return GetValue(TextProperty) as string; }
            set { SetValue(TextProperty, value); }
        }

        public MyBox()
        {
            InitializeComponent();

            this.DataContext = this;

        }  
    }
}
4

1 回答 1

1

只需跟踪列表中的所有框:

IList<MyBox> boxes = new List<MyBox>();

private void b_ClickEntidade(object sender, RoutedEventArgs e)
{
    MyBox c = new MyBox();
    c.Header = "Entidade";
    c.Text = "Atributos";

    ...

    boxes.Add(c);
} 

然后生成整个 XML:

private XElement GetGeneratedXML()
{
    XElement userInformation = new XElement("names");

    foreach (MyBox b in boxes)
    {        
        userInformation.Add(new XElement("first", b.Text));
    }

    return userInformation;
}
于 2013-06-17T14:31:03.203 回答