如何改变一个网格的孩子文本块的字体大小动态地在C#中? [英] How to change the font size of a grid's children text blocks dynamically in c#?

查看:203
本文介绍了如何改变一个网格的孩子文本块的字体大小动态地在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一款Windows Phone使用C#在Microsoft Visual Studio,用下面的代码8.1 WinRT的应用程序,我怎么能动态地更改网格的孩子文本块的字体大小在后面的代码?

In a Windows Phone 8.1 WinRT app using c# in Microsoft Visual Studio, with the following code, how can I change the font size of the grid's children text blocks dynamically in the code behind?

<Grid Name="mainGrid">

    <Grid.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
    </Grid.Resources>

</Grid>



我们的想法是让用户更改选项屏幕字体大小,然后将其保存到当地设置,然后更改显示匹配的字体大小。

The idea is to let the user change the font size in the options screen, and then save it to local settings, and then change the display to match the font size.

网格的孩子文本块动态添加加载应用程序时,我不会问如何从ApplicationData.Current.LocalSettings加载值。我也知道,风格和setter方法没有任何名称然而,如果需要,它可以被填充。

The grid's children text blocks are added dynamically when the app is loaded, and I'm not asking how to load values from ApplicationData.Current.LocalSettings. I'm also aware that the styles and setters don't have any names yet, which could be filled in if needed.

我想避免使用资源字典和数据绑定如果可能的话。

I would like to avoid using a resource dictionary and data bindings if possible.

有人可以提供一个简单的代码示例代码使用后面来改变字体大小?

Can someone provide a simple code example to use in the code behind to change the font size?

推荐答案

下面是我用来动态地改变风格的方式,但资源字典将参与。

Here is the way I used to change the style dynamically, but the resource dictionary would be involved.

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);
}

这篇关于如何改变一个网格的孩子文本块的字体大小动态地在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆