Search code examples
c#wpfmvvmuser-preferences

Setting User Preferences for font family to a wpf mvvm application


How to set User Preferences for font family to a wpf mvvm application in c#?


Solution

  • you can create a global Style for TargetType Window and there set the Preference .

    the resource :

    <Application.Resources>
        <Style TargetType="Window" x:Key="WindowStyle">
            <Setter Property="FontFamily" Value="{Binding FontFamilyPrefernce}" />                      
        </Style>        
    </Application.Resources>
    

    the View :

     <Window Style="{StaticResource WindowStyle}">
          <Grid>
              <TextBox />
          </Grid>
     </Window>
    

    the ViewModel :

        public SomeViewModel()
        {
            FontFamilyPrefernce = new FontFamily("Algerian");
        }
    
        private FontFamily fontFamilyPrefernce;
        public FontFamily FontFamilyPrefernce 
        {
            get {return fontFamilyPrefernce ;}
            set
            {
                fontFamilyPrefernce = value;
                OnPropertyChanged("FontFamilyPrefernce");
            }
        }
    

    hope this helps ..