2013-02-14 58 views
3

我有下面的代碼,我動態創建堆棧面板與其列表框中的孩子。堆棧面板的子值從文本框條目保存WPF Stackpanel Childen

但是我想知道我是否可以將每個孩子值保存到文本或XML文件,並從窗體加載事件中讀取它們。

我知道如何添加項目到常規列表框項目保存和加載文本蒼蠅。所以,我真的想做同樣的過程,以堆棧面板兒童價值太。根據下面的代碼會在寫最後文本錄入

例如,如果我添加文本到文本框,然後按下按鈕,它會添加到堆棧面板的孩子,並將其保存到文件

。但如果我再次輸入文本保存文件日期將覆蓋。

而不是顯示文本框條目的數量,它只顯示最後的文本值。

我對數據綁定的知識較少,因此我不想使用數據庫&只是想將每個孩子的值保存到文件中。

希望你們能幫助我。謝謝!!

這裏是我的代碼

類主窗口

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 

末級

這裏是我的Xmal位碼

<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> 

回答

2

您需要使用重載StreamWriter構造,並指定要追加:

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

如果你不這樣做,該文件將被覆蓋:

真實的數據添加到該文件;假覆蓋文件。如果 指定文件不存在,則此參數不起作用,並且構造函數創建一個新文件。

+0

Thanx bud!感謝您的評論 – 2013-02-14 16:02:42

+0

@RoshiEnd問題解決了嗎? – AbZy 2013-02-14 16:03:10