I am currently developing universal app and I need to handle textbox font size for mobile and desktop separately. I found some approaches but none of them can\'t handle the prob
My problem is that I can't change font sizes defined in styles in the runtime
For your requirement, you could refer Setting
that implemented in Template 10.
Create Setting class that implements INotifyPropertyChanged
and contain FontSize
property
public class Setting : INotifyPropertyChanged
{
private double _fontSize = 20;
public double FontSize
{
get { return _fontSize; }
set { _fontSize = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Make a Setting
instance in your application resources dictionary to init setting during app start.
<Application.Resources>
<ResourceDictionary>
<local:Setting x:Key="Setting"/>
</ResourceDictionary>
</Application.Resources>
Use data binding to bind the FontSize
property to your TextBlocks
like the follow.
<TextBlock Name="MyTextBlock" Text="Hi This is nico" FontSize="{Binding FontSize, Source={StaticResource Setting} }"/>
Change font sizes defined in styles in the runtime.
((Setting)Application.Current.Resources["Setting"]).FontSize = 50;