How do I save Theme of Material Design in a text file for saving color theme chosen in C# WPF?

Mojtaba_Hakim 281 Reputation points
2022-11-20T04:21:11.183+00:00

I'm using Material design. I created a color picker to choose the color the user wants, after the user chooses the color and theme.

I want to save these settings into a text file on the disk. I don't know how can I convert these types to a list for the string which can I use for reading theme that is saved :

 private void MyColorPicker1_PreviewMouseMove(object sender, MouseEventArgs e)  
    {  
            string filepath = @"C:\Themses";  
      
            if (e.LeftButton == MouseButtonState.Pressed)  
            {  
                ITheme theme = _paletteHelper.GetTheme();  
      
                theme.SetPrimaryColor(Color.FromRgb(MyColorPicker1.Color.R, MyColorPicker1.Color.G, MyColorPicker1.Color.B)); //red  
      
                var Test = theme.GetBaseTheme();  
      
                // something here to write all setting inside of ITheme into the text file  
      
                //  
      
                _paletteHelper.SetTheme(theme);  
            }  
    }  

How can I do that?

Full XAML:

 <Window x:Class="WpfApp5.SettingThemsWins.MaterialThemSettingy"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        xmlns:local="clr-namespace:WpfApp5.SettingThemsWins"  
        mc:Ignorable="d"  
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"  
      
        Background="{DynamicResource MaterialDesignPaper}"  
      
        Title="Setting" Height="607" Width="1144" WindowStartupLocation="CenterScreen">  
    <Grid>  
        <materialDesign:ColorPicker x:Name="MyColorPicker1" HorizontalAlignment="Left" Margin="20,17,0,0" VerticalAlignment="Top" Height="353" Width="750" PreviewMouseMove="MyColorPicker1_PreviewMouseMove" />  
        <ToggleButton x:Name="ThemeActivationsBtn"  Style="{StaticResource MaterialDesignSwitchToggleButton}"  ToolTip="Activation Of Dark Theme"  IsChecked="False" Margin="110,380,0,0" Click="ThemeActivationsBtn_Click" HorizontalAlignment="Left" Width="63" Height="27" VerticalAlignment="Top" />  
        <Label Content="Dark Theme :" HorizontalAlignment="Left" Height="24" Margin="20,382,0,0" VerticalAlignment="Top" Width="85"/>  
        <Button x:Name="SaverThemy" Content="Save Theme" HorizontalAlignment="Left" Margin="200,375,0,0" VerticalAlignment="Top" Width="170" Click="SaverThemy_Click"/>  
    </Grid>  
    </Window>  

Code Behind:

using MaterialDesignThemes.Wpf;  
using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Data;  
using System.Windows.Documents;  
using System.Windows.Input;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
using System.Windows.Shapes;  
  
namespace WpfApp5.SettingThemsWins  
{  
    /// <summary>  
    /// Interaction logic for MaterialThemSettingy.xaml  
    /// </summary>  
    public partial class MaterialThemSettingy : Window  
    {  
        private readonly PaletteHelper _paletteHelper = new PaletteHelper();  
        bool isDark;  
  
        public MaterialThemSettingy()  
        {  
            InitializeComponent();  
            //EmptySampleWind.Window1 window1 = new EmptySampleWind.Window1();  
            //window1.Show();  
        }  
  
        public static IEnumerable<string> SortByLength(IEnumerable<string> e)  
        {  
            // Use LINQ to sort the array received and return a copy.  
            var sorted = from s in e  
                         orderby s.Length ascending  
                         select s;  
            return sorted;  
        }  
   
        private void MyColorPicker1_PreviewMouseMove(object sender, MouseEventArgs e)  
        {  
            string filepath = @"C:\Themses";  
  
            if (e.LeftButton == MouseButtonState.Pressed)  
            {  
                ITheme theme = _paletteHelper.GetTheme();  
  
                theme.SetPrimaryColor(Color.FromRgb(MyColorPicker1.Color.R, MyColorPicker1.Color.G, MyColorPicker1.Color.B)); //red  
  
                var Test = theme.GetBaseTheme();  
  
                // something here to write all setting inside of ITheme into the text file  
  
                //  
  
                _paletteHelper.SetTheme(theme);  
            }  
        }  
  
        private void ThemeActivationsBtn_Click(object sender, RoutedEventArgs e)  
        {  
            isDark = (bool)ThemeActivationsBtn.IsChecked;  
  
            if (isDark)  
            {  
                ITheme theme = _paletteHelper.GetTheme();  
                IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();  
                theme.SetBaseTheme(baseTheme);  
  
                _paletteHelper.SetTheme(theme);  
            }  
            else  
            {  
                ITheme theme = _paletteHelper.GetTheme();  
                IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();  
                theme.SetBaseTheme(baseTheme);  
  
                _paletteHelper.SetTheme(theme);  
            }  
        }  
    }  
}  

How can I do that ?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2022-11-21T09:09:42.737+00:00

    Hi,@Mojtaba_Hakim . You could try to refer to the code below and combine it to get the selected color of ColorPicker.

    I use the following code to transfer the color data to a txt file in json format. But I'm not familiar with Material Design's ColorPicker, and I don't know how to get the selected color.
    Or could you share how to get the ColorPicker's selected color?

    262480-materialdesign.txt

    The result:
    262500-image.png

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful