silverlight将文本框保存为xml [英] silverlight save textbox to xml
问题描述
我的项目看起来像这样, 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文件(* .xml)| * .xml |所有文件(*。*)| *。*;
saveFileDialog.FilterIndex = 1;
if(saveFileDialog.ShowDialog()== true)
{
使用(Stream stream = saveFileDialog.OpenFile())
{
StreamWriter sw = new StreamWriter(stream,System.Text.Encoding.UTF8);
sw.Write(GetGeneratedXML()。ToString());
sw.Close();
stream.Close();
}
}
}
私人XElement GetGeneratedXML()
{
XElement userInformation = new XElement(names);
userInformation.Add(new XElement(first,box1.Text));
// userInformation.Add(new XElement(last,lastNameText.Text));
return userInformation;
}
但是这是从已经在XAML中创建的文本框(那个我只是用于测试),我想要的是保存通过单击按钮创建的所有文本框的文本。
这是如何创建文本框架: / p>
XAML:
< TextBox Text ={Binding Header, UpdateSourceTrigger = PropertyChanged}
BorderBrush =黑色BorderThickness =1/>
< TextBox Text ={Binding Text,UpdateSourceTrigger = PropertyChanged}
TextWrapping =Wrap
VerticalScrollBarVisibility =Auto
AcceptsReturn =True
BorderBrush =BlackBorderThickness =1Grid.Row =1/>
C#:
code> 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;
}
}
}
只需跟踪列表中的所有框:
IList< MyBox> box = 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)
{
userInformation.Add(new XElement(first,b.Text));
}
return userInformation;
}
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;
}
}
}
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;
}
这篇关于silverlight将文本框保存为xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!