Search code examples
c#wpfwpf-controlsbinarywriter

Saving WPF Button Properties using BinaryWriter


Is it possible to do? I am having trouble with finding a way of saving WPF Button's properties using BinaryWriter for a text-based game. The thing is I need it to save button's button1.Content, button1.Click and button1.ToolTip so I can load it back using BinaryReader.


Solution

  • You can save entire Button, which will be persisted with all its properties.

    And you can then de-serialize it back. We can the services provided by using System.Windows.Markup namespace.

    1. Serialize Button with name Btn

      string btnData = XamlWriter.Save(Btn);

    2. Deserialize it back

      Button btn = (Button) XamlReader.Parse(btnData);