How to easily allow users to update Styles used be elements in XAML (UWP)

前端 未结 1 875
你的背包
你的背包 2020-12-18 17:30

This is for a Windows 10 UWP. I need to allow users to update values on Styles that are associated with elements used throughout the application (i.e allow users to change t

相关标签:
1条回答
  • 2020-12-18 18:10

    Unfortunately this kind of user-defined styling is not easily available in UWP. You can however implement a kind of styling solution using data binding.

    First step is to create a class like CustomUISettings which implements INotifyPropertyChanged and has properties like HeaderFontSize, etc.

    Now on app start create an instance of this class and add it as app resource:

    Application.Current.Resources["CustomUISettings"] = new CustomUISettings();
    

    Now you can bind to the properties in this class anywhere in your code:

    <TextBox FontSize="{Binding HeaderFontSize, Source={StaticResource CustomUISettings}}" />
    

    You must use the classic {Binding} markup extension, because {x:Bind} does not support Source setting.

    To modify the UI settings you can just retrieve the instance anywhere and set the properties as you see fit:

    var customUISettings = (CustomUISettings)Application.Current.Resources["CustomUISettings"];
    customUISettings.HeaderFontSize = 50;
    

    You must make sure that all properties in CustomUISettings class fire the PropertyChanged event. You can see how to implement INotifyPropertyChanged interface for example here.

    0 讨论(0)
提交回复
热议问题