如何在 C# 中动态更改网格子文本块的字体大小?
How to change the font size of a grid's children text blocks dynamically in c#?
在 Microsoft Visual Studio 中使用 c# 的 Windows Phone 8.1 WinRT 应用程序中,如何使用以下代码动态更改网格子文本块的字体大小背后的代码?
<Grid Name="mainGrid">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="5"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</Grid.Resources>
</Grid>
想法是让用户在选项屏幕中更改字体大小,然后将其保存到本地设置,然后更改显示以匹配字体大小。
网格的子文本块是在加载应用程序时动态添加的,我不是在问如何从 ApplicationData.Current.LocalSettings 加载值。我也知道样式和设置器还没有任何名称,如果需要可以填写。
我希望尽可能避免使用资源字典和数据绑定。
有人可以提供一个简单的代码示例以在后面的代码中使用来更改字体大小吗?
这里是我以前动态改变样式的方式,但是会涉及到资源字典
private void changeSzie_Click(object sender, RoutedEventArgs e)
{
var dynamicStyle = new Windows.UI.Xaml.Style();
var targetType = typeof(Windows.UI.Xaml.Controls.TextBlock);
dynamicStyle.TargetType = targetType;
dynamicStyle.Setters.Add(new Setter(Windows.UI.Xaml.Controls.TextBlock.FontSizeProperty, int.Parse(textbox.Text)));
if (mainGrid.Resources.Keys.Contains(targetType))
{
mainGrid.Resources.Remove(targetType);
}
mainGrid.Resources.Add(targetType, dynamicStyle);
}
在 Microsoft Visual Studio 中使用 c# 的 Windows Phone 8.1 WinRT 应用程序中,如何使用以下代码动态更改网格子文本块的字体大小背后的代码?
<Grid Name="mainGrid">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="5"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</Grid.Resources>
</Grid>
想法是让用户在选项屏幕中更改字体大小,然后将其保存到本地设置,然后更改显示以匹配字体大小。
网格的子文本块是在加载应用程序时动态添加的,我不是在问如何从 ApplicationData.Current.LocalSettings 加载值。我也知道样式和设置器还没有任何名称,如果需要可以填写。
我希望尽可能避免使用资源字典和数据绑定。
有人可以提供一个简单的代码示例以在后面的代码中使用来更改字体大小吗?
这里是我以前动态改变样式的方式,但是会涉及到资源字典
private void changeSzie_Click(object sender, RoutedEventArgs e)
{
var dynamicStyle = new Windows.UI.Xaml.Style();
var targetType = typeof(Windows.UI.Xaml.Controls.TextBlock);
dynamicStyle.TargetType = targetType;
dynamicStyle.Setters.Add(new Setter(Windows.UI.Xaml.Controls.TextBlock.FontSizeProperty, int.Parse(textbox.Text)));
if (mainGrid.Resources.Keys.Contains(targetType))
{
mainGrid.Resources.Remove(targetType);
}
mainGrid.Resources.Add(targetType, dynamicStyle);
}