Posted 13 September 2021, 6:27 pm EST
Hi Mike,
You can save FontSize and FontFamily at the time of window closing. For that you need to find C1FontFamilyTool and C1FontSizeTool from C1RichTextBoxToolbar’s template in Closing event handler of Window and then you can save SelectedValue in XML :
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(rtbToolbar.IsLoaded)
{
C1FontFamilyTool fontFamily = (C1FontFamilyTool)rtbToolbar.Template.FindName("13_T", rtbToolbar);
C1FontSizeTool fontSize = (C1FontSizeTool)rtbToolbar.Template.FindName("15_T", rtbToolbar);
using (XmlWriter writer = XmlWriter.Create(fileName))
{
ToolbarSetting setting = new ToolbarSetting();
if(fontSize.SelectedValue!=null & fontFamily.SelectedValue!=null)
{
setting.SavedFontSize = fontSize.SelectedValue.ToString();
setting.SavedFontFamily = fontFamily.SelectedValue.ToString();
System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(ToolbarSetting));
xml.Serialize(writer, setting);
}
}
}
}
You can load previously save FontSize and FontFamily. For that you need to handle Loaded event of C1RichTextBoxToolbar and then you can set SelectedValue of C1FontFamilyTool and C1FontSizeTool( which can get from toolbar’s template) :
private void ToolBar_Loaded(object sender, RoutedEventArgs e)
{
C1RichTextBoxToolbar toolbar = sender as C1RichTextBoxToolbar;
C1FontFamilyTool fontFamily = (C1FontFamilyTool)toolbar.Template.FindName("13_T", toolbar);
C1FontSizeTool fontSize = (C1FontSizeTool)toolbar.Template.FindName("15_T", toolbar);
try
{
using (StreamReader reader = File.OpenText(fileName))
{
ToolbarSetting setting = new ToolbarSetting();
System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(ToolbarSetting));
setting = (ToolbarSetting)xml.Deserialize(reader);
fontSize.SelectedValue = Double.Parse(setting.SavedFontSize);
fontFamily.SelectedValue = new FontFamily(setting.SavedFontFamily);
}
}
catch (Exception ex)
{
fontSize.SelectedIndex = 0;
fontFamily.SelectedIndex = 0;
}
}
Please refer the attached sample for the same : RichTextBoxToolbar.zip
Best Regards,
Nitin