Save WPF Stackpanel Childen

I have the following codes and I have dynamically created a Stack pane with its children inside a List box. The value of the children of the stack pane is obtained from the input of the text box

however I am wondering if I can store the value of each child in a text or XML file and read them from the form load event.

I know how to add items to the "Normal List" item, save and load from text flies. therefore, I really wanted to do the same procedure for the value of the children of the stack pane.

according to the following codes, it will write the last entry in the textbox

for example if I add some text to the textbox and click the button it will add to the children of the stack pane and save them to a file. but if I enter text again, saving the file date is overwritten.

instead of showing the input number of the text box, it only displays the last text value.

I have less knowledge of data binding, so I don't want to use a database and just want to store the value for each child in the file.

Hope you guys can help me. Thank!!

here are my codes

MainWindow class

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim st As New StackPanel()
    Dim tb As New Label()

    tb.Content = TextBox1.Text

    st.HorizontalAlignment = Windows.HorizontalAlignment.Left
    'st.Height = 40
    'st.Width = 40
    'st.Margin = New Thickness(45, 45, 45, 0)

    Me.ListBox1.Items.Add(st)
    st.Children.Add(tb)



    IO.Directory.CreateDirectory("D:\save")
    Dim w As New IO.StreamWriter("D:\save\test.text")

    ' Dim i As Integer

    For Each tb In st.Children
        w.WriteLine(tb.Content)

    Next


    w.Close()
End Sub

      

End class

here are my xmal codes

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="509" Width="762">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="320*" />
        <ColumnDefinition Width="420*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="49*" />
        <RowDefinition Height="262*" />
    </Grid.RowDefinitions>
    <TextBox Height="25" HorizontalAlignment="Left" Margin="59,244,0,0" Name="TextBox1" VerticalAlignment="Top" Width="121" Grid.Row="1" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="59,275,0,0" Name="TextBox2" VerticalAlignment="Top" Width="121" Grid.Row="1" />
    <Button Content="Button" Height="24" HorizontalAlignment="Left" Margin="77,308,0,0" Name="Button1" VerticalAlignment="Top" Width="83" Grid.Row="1" />
    <Label Content="Label" Grid.Column="1" Height="29" HorizontalAlignment="Left" Margin="38,12,0,0" Name="Label1" VerticalAlignment="Top" Width="120" DataContext="{Binding}" Visibility="Hidden" />
    <ListBox Height="297" HorizontalAlignment="Left" Margin="12,12,0,0" Name="ListBox1" VerticalAlignment="Top" Width="294" Grid.RowSpan="10" FontSize="14" FontStretch="Expanded" MinHeight="10"></ListBox>
</Grid>

      

+3


source to share


1 answer


You need to use the overloaded StreamWriter constructor and specify what you want to add:

Dim w As New IO.StreamWriter("D:\save\test.text",True)

      



If you don't, the file will be overwritten:

true to add data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect and the constructor creates a new file.

+2


source







All Articles