Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
保存WPF StackPanel子级_Wpf_Vb.net_Listbox_Children_Stackpanel - Fatal编程技术网

保存WPF StackPanel子级

保存WPF StackPanel子级,wpf,vb.net,listbox,children,stackpanel,Wpf,Vb.net,Listbox,Children,Stackpanel,我有以下代码,并在列表框中动态创建了堆栈面板及其子项。堆栈面板的子项值来自文本框条目 但是,我想知道我是否可以将每个子项值保存到文本或xml文件中,并从form load事件中读取它们 我知道如何将项目添加到常规列表框项目保存和从文本框加载。 所以,我真的想对堆栈面板的子值也执行相同的过程 根据以下代码,它将覆盖最后一个文本框条目 例如,如果我将文本添加到文本框并按下按钮,它将添加到堆栈面板子项并将其保存到文件中。但如果我再次输入文本,保存文件的日期将被覆盖 它不显示文本框条目的编号,而是仅显示

我有以下代码,并在列表框中动态创建了堆栈面板及其子项。堆栈面板的子项值来自文本框条目

但是,我想知道我是否可以将每个子项值保存到文本或xml文件中,并从form load事件中读取它们

我知道如何将项目添加到常规列表框项目保存和从文本框加载。 所以,我真的想对堆栈面板的子值也执行相同的过程

根据以下代码,它将覆盖最后一个文本框条目

例如,如果我将文本添加到文本框并按下按钮,它将添加到堆栈面板子项并将其保存到文件中。但如果我再次输入文本,保存文件的日期将被覆盖

它不显示文本框条目的编号,而是仅显示最后一个文本值

我对数据绑定知之甚少,所以我不想使用数据库&只想将每个子项的值保存到文件中

希望你们能帮助我。谢谢

这是我的密码

类主窗口

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>


您需要使用重载构造函数并指定要追加的内容:

Dim w As New IO.StreamWriter("D:\save\test.text",True)
如果不这样做,文件将被覆盖:

true将数据追加到文件中;false将覆盖该文件。如果 指定的文件不存在,此参数无效,并且 构造函数创建一个新文件


塔克斯巴德!感谢您的评论