保存并加载用户控件

时间:2016-08-18 13:37:09

标签: c# wpf user-controls

所以我正在尝试构建一个WPF应用程序,但我遇到了一些争吵。

所以我有以下代码:

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

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

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

因此,当用户与UI进行交互时,他会将几个SupplierUc添加到OfferStackPanel,并将几个CategoryUc添加到每个SupplierUc。

我需要保存最终的OfferStackPanel并随时将其重新加载到项目中,并可以编辑它。

2 个答案:

答案 0 :(得分:0)

你可能正在寻找的东西:

Creating and Loading XAML dynamically in WPF

答案 1 :(得分:0)

Ed实际上给了我使用POCO clases的想法,我设法通过创建自定义类并将用户输入的信息映射到它们然后序列化为xml文件来做我想要的。当我加载文件时,我在模型中反序列化它,然后使用xml中包含的数据重建元素。