silverlight save textbox to xml

感情迁移 提交于 2019-12-24 07:18:20

问题


my project looks like this, http://s23.postimg.org/mrhuocn4b/asd.png

and I already can save a textbox to xml file, using this code:

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;

    }

But this is from a textbox already created in XAML (that I used just for testing), and what I want is to save the text's of all the textboxes created by clicking the buttons.

THIS IS HOW I CREATE THE TEXTBOXES:

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

EDIT -----------

THIS IS 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;

        }  
    }
}

回答1:


Just keep track of all the boxes in a list:

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

Then generate the whole XML:

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

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

    return userInformation;
}


来源:https://stackoverflow.com/questions/17125665/silverlight-save-textbox-to-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!