Articles in this section
Category / Section

How to save and retreive the required properties for a control?

1 min read

In general, you can save and retrieve the required properties of a control by using the XML Serialization and Deserialization in WPF.

For example, the following code demonstrates the saving and retrieving of the background color of an IntegerTextBox by using the XML Serialization and Deserialization.

XAML

<Window.Resources>
        <syncfusion:ColorToBrushConverter x:Key="ColorToBrushConverter"/>
</Window.Resources>
 <Grid>
       <StackPanel>
        <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="Select color" Width="200"/>
                <syncfusion:ColorPickerPalette Width="200" x:Name="colorPalette"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="IntegerTextBox" Width="200"/>
                <syncfusion:IntegerTextBox Background="{Binding ElementName=colorPalette,Path=Color,Converter={StaticResource ColorToBrushConverter}}" x:Name="box"/>
            </StackPanel>
        </StackPanel>
    </Grid>

C#

public partial class MainWindow : Window
    {
        Data userchoices;
        public MainWindow()
        {
            InitializeComponent();
            userchoices = new Data();
            Loaded += MainWindow_Loaded;
            Closing +=MainWindow_Closing;   
        }
        public static void Serialization(string xmlPath, Data tasks)
        {
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(Data));
                using (StreamWriter wr = new StreamWriter(xmlPath))
                {
                    xs.Serialize(wr, tasks);
                }
            }
            catch (Exception)
            {
            }
        }
        public static Data DeSeralization(string xmlPath)
        {
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(Data));
                using (StreamReader rd = new StreamReader(xmlPath))
                {
                    return xs.Deserialize(rd) as Data;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            userchoices = new Data() { BackColor = colorPalette.Color.ToString() };
            Serialization(".xml", userchoices);
        }
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var choices = DeSeralization(".xml");
            if (colorPalette != null && choices.BackColor != "Color")
            {
                colorPalette.Color = (Color)System.Windows.Media.ColorConverter.ConvertFromString(choices.BackColor);
            }
        }
    }
    public class Data
    {
        private string backColor;
        public string BackColor
        {
            get { return backColor; }
            set { backColor = value; }
        }                
    }

Conclusion

I hope you enjoyed learning about how to save and retrieve the required properties for a WPF.

You can refer to our WPF feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied