存储网格及其子控件到数据库并在另一个窗口中检索它

时间:2014-04-17 13:33:37

标签: wpf vb.net grid xmlserializer datacontractserializer

我在窗口中有主网格,它包含我想要将其存储在数据库中的所有数据,并在另一个窗口中检索它。我的网格保持跟随控制:

 <Grid Name="myGrid" Margin="0,1,0,-1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="360*"/>
            <ColumnDefinition Width="153*"/>
            <ColumnDefinition Width="739*"/>
        </Grid.ColumnDefinitions>
        <MediaElement HorizontalAlignment="Left" Height="587" Margin="24,46,0,0" VerticalAlignment="Top" Width="797" Grid.ColumnSpan="3"/>
        <Label Content="Dishes" HorizontalAlignment="Left" Margin="353,132,0,0" VerticalAlignment="Top" Width="75" FontSize="16" Name = "Dishes" Height="30" Grid.Column="2"/>
        <Label Content="Quantities" Height="30" HorizontalAlignment="Left" Margin="503,130,0,0" VerticalAlignment="Top" Width="90" FontSize="16" Name="Quantities" Grid.Column="2"/>
        <Label Content="Price" Height="30" HorizontalContentAlignment ="Center"  Margin="634,131,10,0" VerticalAlignment="Top" FontSize="16" Name="Price" Grid.Column="2"/>
        <Label Content="Totel Before Tax" HorizontalAlignment="Left" Height="32" Margin="324,487,0,0" VerticalAlignment="Top" Width="210" FontSize="20" MouseDoubleClick="Dishes_MouseDoubleClick" Grid.Column="2"/>
        <Label Content="950" HorizontalContentAlignment ="Center"  Margin="629,487,10,0" VerticalAlignment="Top" Width="100" FontSize="20" MouseDoubleClick="Dishes_MouseDoubleClick" Grid.Column="2"/>
        <Label Content="Discount" HorizontalAlignment="Left" Margin="324,529,0,0" VerticalAlignment="Top" Width="128" Height="35" FontSize="20" MouseDoubleClick="Dishes_MouseDoubleClick" Grid.Column="2"/>
        <Label Content="10%" HorizontalContentAlignment="Center"  Margin="634,524,10,0" VerticalAlignment="Top" Width="95" FontSize="20" MouseDoubleClick="Dishes_MouseDoubleClick" Grid.Column="2"/>
        <Label Content="Tax" HorizontalAlignment="Left" Margin="324,569,0,0" VerticalAlignment="Top" Width="119" FontSize="20" MouseDoubleClick="Dishes_MouseDoubleClick" Grid.Column="2"/>
        <Label Content="20%" HorizontalContentAlignment="Center"  Margin="634,566,10,0" VerticalAlignment="Top" Width="95" FontSize="20" MouseDoubleClick="Dishes_MouseDoubleClick" Grid.Column="2"/>

        <Image HorizontalAlignment="Left" Height="587" Margin="31,42,0,0" VerticalAlignment="Top" Width="797" Source="Lighthouse.jpg" Stretch="Fill" Grid.ColumnSpan="3" MouseLeftButtonDown="Image_MouseLeftButtonDown_1"/>
        <Image HorizontalAlignment="Left" Height="114" Margin="490,0,0,0" VerticalAlignment="Top" Width="132" Source="logo.png" Stretch="Fill" Grid.Column="2" MouseLeftButtonDown="Image_MouseLeftButtonDown_1"/>
        <Label Content="Advertisement" HorizontalAlignment="Left" Margin="24,4,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.54,-0.363" Width="196" FontSize="20"/>
        <Label Content="Grand Total" HorizontalAlignment="Left" Height="37" Margin="324,606,0,0" VerticalAlignment="Top" Width="210" FontSize="20" MouseDoubleClick="Dishes_MouseDoubleClick" Grid.Column="2"/>
        <Label Content="1045" HorizontalContentAlignment ="Center"  Margin="629,608,10,0" VerticalAlignment="Top" Width="100" FontSize="20" Grid.Column="2"/>

        <Label Content="Kaju Kari" Height="30" HorizontalAlignment="Left" HorizontalContentAlignment="Center"  Margin="342,180,0,0" VerticalAlignment="Top" Width="103" FontSize="16" Grid.Column="2" Tag="1" MouseDoubleClick="Label_MouseDoubleClick_1"/>
        <Label Content="2" HorizontalAlignment="Left" Margin="511,177,0,0" VerticalAlignment="Top" Width="69" Height="30" FontSize="16" Grid.Column="2" Tag="100" MouseDoubleClick="Label_MouseDoubleClick_1"/>
        <Label Content="240" HorizontalAlignment="Left" Margin="663,179,0,0" VerticalAlignment="Top" Width="37" FontSize="16" Grid.Column="2" Tag="200" MouseDoubleClick="Label_MouseDoubleClick_1"/>
        <Label Content="Paneer Kadai" Height="30" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="341,213,0,0" VerticalAlignment="Top" Width="103" FontSize="16" Grid.Column="2" Tag="2"/>
        <Label Content="2" HorizontalAlignment="Left" Margin="510,214,0,0" VerticalAlignment="Top" Width="69" Height="29" RenderTransformOrigin="0.652,0.586" FontSize="16" Grid.Column="2" Tag="101" MouseDoubleClick="Label_MouseDoubleClick_1"/>
        <Label Content="210" HorizontalAlignment="Left" Margin="664,213,0,0" VerticalAlignment="Top" FontSize="16" Grid.Column="2" Tag="201" MouseDoubleClick="Label_MouseDoubleClick_1"/>
        <Label Content="Better Roti" Height="30" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="340,249,0,0" VerticalAlignment="Top" Width="104" FontSize="16" Grid.Column="2" Tag="3"/>
        <Label Content="5" HorizontalAlignment="Left" Margin="510,248,0,0" VerticalAlignment="Top" Width="68" Height="28" FontSize="16" Grid.Column="2" Tag="102" MouseDoubleClick="Label_MouseDoubleClick_1"/>
        <Label Content="10" HorizontalAlignment="Left" Margin="665,248,0,0" VerticalAlignment="Top" Width="35" FontSize="16" Grid.Column="2" Tag="202" MouseDoubleClick="Label_MouseDoubleClick_1"/>
        <Button Content="Write" Grid.Column="2" HorizontalAlignment="Left" Margin="647,14,0,0" VerticalAlignment="Top" Width="75" Name="btnWrite"/>

    </Grid> 

1 个答案:

答案 0 :(得分:1)

虽然这不能直接回答您的问题,但您确实需要知道这一点。在WPF中,我们从不保存UI元素。相反,我们创建数据类型类,数据将类属性绑定到UI控件属性。通过这种方式,我们可以保存数据,而不是UI元素。通过这种方式,您可以执行类似的操作(假设您创建了一个具有相关属性的类):

<Grid Name="myGrid" Margin="0,1,0,-1" DataContext="{Binding YourDataTypeProperty}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="360*"/>
        <ColumnDefinition Width="153*"/>
        <ColumnDefinition Width="739*"/>
    </Grid.ColumnDefinitions>
    <MediaElement HorizontalAlignment="Left" Height="587" Margin="24,46,0,0" VerticalAlignment="Top" Width="797" Grid.ColumnSpan="3"/>
    <Label Content="{Binding Dishes}" HorizontalAlignment="Left" Margin="353,132,0,0" VerticalAlignment="Top" Width="75" FontSize="16" Name = "Dishes" Height="30" Grid.Column="2"/>
    <Label Content="{Binding Quantities}" Height="30" HorizontalAlignment="Left" Margin="503,130,0,0" VerticalAlignment="Top" Width="90" FontSize="16" Name="Quantities" Grid.Column="2"/>
    <Label Content="{Binding Price}" Height="30" HorizontalContentAlignment ="Center"  Margin="634,131,10,0" VerticalAlignment="Top" FontSize="16" Name="Price" Grid.Column="2"/>
    <Label Content="{Binding TotalBeforeTax}" HorizontalAlignment="Left" Height="32" Margin="324,487,0,0" VerticalAlignment="Top" Width="210" FontSize="20" MouseDoubleClick="Dishes_MouseDoubleClick" Grid.Column="2"/>
    ...
    <Button Content="Write" Grid.Column="2" HorizontalAlignment="Left" Margin="647,14,0,0" VerticalAlignment="Top" Width="75" Name="btnWrite"/>
</Grid> 

然后,一旦重新加载数据,只需将其设置为YourDataTypeProperty属性,UI控件将自动从数据绑定对象中呈现正确的值。有关WPF中数据绑定的更多信息,请参阅MSDN上的Data Binding Overview页面。