简体   繁体   English

保存并加载用户控件

[英]Save and load User Control

So I am trying to build a WPF app and I ran into a bit of a scramble. 因此,我尝试构建WPF应用程序,但遇到了一些麻烦。

So I have the following code: 所以我有以下代码:

    <StackPanel Name="OfferStackPanel">
                    <TextBlock Name="OfferNameTextBlock"
                               Text=""
                               Margin="2,10,2,10"
                               HorizontalAlignment="Center"
                               TextAlignment="Center"
                               FontSize="20"
                               MaxWidth="800"
                               TextWrapping="Wrap"/>
                    <StackPanel Name="SuppliersStackPanel"
                        MinWidth="1302"
                        MaxWidth="1302"
                ></StackPanel>
                    <Image x:Name="Logo" 
                           HorizontalAlignment="Right" Source="logo.ico"
                           MaxWidth="100"
                           Margin="20,20,10,20"
                           />
</StackPanel>

and

<Grid Name="SupplierUc">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="13"/>
            <ColumnDefinition Width="102"/>
            <ColumnDefinition Width="360*"/>
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="1"
                   Grid.Row="0"
                   Text="Producator"
                   VerticalAlignment="Center"
                   ></TextBlock>
        <TextBox Name="SupplierNameTextBox" 
            Grid.Column="2"
                 Grid.Row="0"
                 FontStyle="Italic"
                 VerticalContentAlignment="Center"
                 FontSize="25"
                 MaxWidth="300"
                 Width="300"
                 Foreground="Red"
                 HorizontalAlignment="Left"
                 Margin="0,3,0,3"
                 />
        <Button Name="AddCategoryButton"
                Click="AddCategoryUc"
                Grid.Column="1"
                Grid.ColumnSpan="2"
                Grid.Row="1"
                FontSize="15"
                Height="30"
                MaxHeight="30"
                Width="130"
                MaxWidth="130"
                Content="Adauga Categorie"
                HorizontalAlignment="Left"
                Background="{x:Null}" 
                BorderBrush="{x:Null}" 
                Foreground="#FF6270F5" Cursor="Hand"
                />

        <Button Name="DeleteSupplierRowButton"
                Grid.Row="0"
                Grid.Column="3"
                Content="Sterge Producator" 
                Background="{x:Null}" 
                BorderBrush="{x:Null}" 
                Click="RemoveSupplierUc"
                Foreground="#FFF50F0F"
                Cursor="Hand"
            />
            <StackPanel Grid.Row="2"
                    Grid.Column="1"
                    Grid.ColumnSpan="3"
                    Name="CategoryPanel"
            ></StackPanel>
    </Grid>

and

<Grid Name = "CategoryUC">
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="13"/>
            <ColumnDefinition Width="102"/>
            <ColumnDefinition Width="360*"/>
            <ColumnDefinition Width="100*" />
            <ColumnDefinition Width="100"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="1"
                   Grid.Row="0"
                   Text="Categorie"
                   VerticalAlignment="Center"
                   ></TextBlock>
        <TextBox Name="CategoryNameTextBox"
                Grid.Column="2"
                 Grid.ColumnSpan="2"
                 Grid.Row="0"
                 FontSize="20"
                 VerticalContentAlignment="Center"
                 MinWidth="300"
                 Width="Auto"
                 Foreground="Black"
                 HorizontalAlignment="Left"
                 Margin="0,3,0,3"
                 />
        <Button Name="DeleteSupplierRowButton"
                Grid.Row="0"
                Grid.Column="4"
                Content="Sterge Categorie" 
                Background="{x:Null}" 
                BorderBrush="{x:Null}" 
                Click="RemoveCategoryUc"
                Foreground="#FFF50F0F"
                Cursor="Hand"
            />
        <Button Name="AddOptionUcButton"
                Grid.Column="1"
                Grid.Row="1"
                Content="Adauga Optiune"
                Click="AddOptionUc"
                Height="20"
                Width="100"
                Background="{x:Null}" 
                BorderBrush="{x:Null}"
                Foreground="Black"
                Cursor="Hand"
                Margin="1,1,1,1"/>
        <UniformGrid Grid.Row="2"
                     Columns="2"
                    Grid.Column="1"
                    Grid.ColumnSpan="4"
                    ClipToBounds="True"
                    Name="OptionItems">
        </UniformGrid>
    </Grid>

So, when the user will interact with the UI, he will dinamically add several SupplierUc to the OfferStackPanel and several CategoryUc to each SupplierUc. 因此,当用户与UI进行交互时,他将在动力学上将几个SupplierUc添加到OfferStackPanel,并将几个CategoryUc添加到每个SupplierUc。

I need to save the final OfferStackPanel and reload it into the project at any time, with the possibility to edit it. 我需要保存最终的OfferStackPanel并随时将其重新加载到项目中,并可以对其进行编辑。

That's what you are probably looking for : 那就是您可能要寻找的东西:

Creating and Loading XAML dynamically in WPF 在WPF中动态创建和加载XAML

Ed actually gave me the idea to use POCO clases and I managed to do what I wanted by creating custom classes and mapping the information the user inputs to them and then serializing into xml files. Ed实际上给了我使用POCO分类的想法,我设法通过创建自定义类并将用户输入的信息映射到它们,然后序列化为xml文件来完成我想要的事情。 When I load a file, I deserialize it in the models and then rebuild the elements with the data contained in the xml. 加载文件时,我会在模型中反序列化该文件,然后使用xml中包含的数据重建元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM