在uwp中动态设置字体大小为richtexblock [英] Set Font size to richtexblock in uwp dynamically

查看:30
本文介绍了在uwp中动态设置字体大小为richtexblock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<ScrollViewer x:Name="swipeBetweenPages" Grid.Row="1">
    <Pivot DataContext="{StaticResource ViewModel}" x:Name="pivot" Margin="0,-45,0,0" 
                     HeaderTemplate="{StaticResource headerTest}" 
           ItemTemplate="{StaticResource pivotTemplate}" ItemsSource="{Binding Articles}" SelectionChanged="pivot_SelectionChanged">
    </Pivot>
</ScrollViewer>

<Page.Resources>
    <ViewModels:ArticleViewModel x:Key="ViewModel" />
    <DataTemplate x:Key="headerTest">
    </DataTemplate>
    <DataTemplate x:Key="pivotTemplate">
        <StackPanel Margin="-15 0 -15 0">
            <Grid>
                <Grid.Background>
                    <ImageBrush AlignmentX="Center" AlignmentY="Center" ImageSource="Assets/PlaceHolder.jpg"></ImageBrush>
                </Grid.Background>
                <Image q42controls:ImageExtensions.CacheUri="{Binding ImageURL}" Tag="{Binding ImageURL}" Tapped="ImageView"></Image>
            </Grid>
            <StackPanel Background="White">
                <TextBlock x:Name="HeadLine" Text="{Binding HeadLine}"  
                                           Margin="10 5 0 -5" TextWrapping="Wrap" 
                                           FontSize="20" Foreground="Black"
                                           FontFamily="{StaticResource HeadlineCommonFamiy}"
                                           Pivot.SlideInAnimationGroup="GroupTwo" Height="63"
                                           FontWeight="Bold" TextTrimming="CharacterEllipsis"/>
                <TextBlock Text="{Binding Abstract}" TextWrapping="Wrap" FontSize="15" FontStyle="Italic"
                               Pivot.SlideInAnimationGroup="GroupTwo" Margin="10 5 0 10"
                                       FontFamily="{StaticResource AbstractCommonFamily}"/>                   
            </StackPanel>
            <StackPanel x:Name="descriptionSP" Background="White">
            <RichTextBlock IsTextSelectionEnabled="False" x:Name="richTextBlock" 
                           local:Properties.Html="{Binding ArticleDetail}" TextWrapping="Wrap"
                           Pivot.SlideInAnimationGroup="GroupTwo" Margin="10 5 0 10"
                                   FontFamily="{StaticResource ContentControlThemeFontFamily}">
            </RichTextBlock>
        </StackPanel>
        </StackPanel>
    </DataTemplate>
</Page.Resources>

我正在尝试在后端动态地将字体大小设置为富文本块.

I am trying to set the font size to rich text block in the back end dynamically.

现在,我在 C# 端尝试使用以下代码:

Now, I am trying with the following code in the C# end:

 private T FindElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
    var count = VisualTreeHelper.GetChildrenCount(parentElement);
    if (count == 0) return null;

    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(parentElement, i);
        if (child != null && child is T)
            return (T)child;
        else
        {
            var result = FindElementInVisualTree<T>(child);
            if (result != null)
                return result;
        }
    }
    return null;
}        

RichTextBlock richTextBlock = new RichTextBlock();
StackPanel rootStackPanel = new StackPanel();
StackPanel childStackPanel = new StackPanel();

PivotItem item = (sender as Pivot).ContainerFromItem((sender as Pivot).SelectedItem) as PivotItem;
rootStackPanel = item.ContentTemplate.LoadContent() as StackPanel;
childStackPanel = rootStackPanel.FindName("descriptionSP") as StackPanel;
richTextBlock = rootStackPanel.FindName("richTextBlock") as RichTextBlock;

Paragraph paragraph = new Paragraph();
Run run = new Run();
// Customize some properties on the RichTextBlock.
richTextBlock.IsTextSelectionEnabled = true;
richTextBlock.SelectionHighlightColor = new SolidColorBrush(Windows.UI.Colors.Pink);
richTextBlock.Foreground = new SolidColorBrush(Windows.UI.Colors.Blue);
richTextBlock.FontWeight = Windows.UI.Text.FontWeights.Light;
richTextBlock.FontFamily = new FontFamily("Arial");
richTextBlock.FontStyle = Windows.UI.Text.FontStyle.Italic;
richTextBlock.FontSize = 50;
//run.Text = "This is some sample text to demonstrate some properties.";          
paragraph.Inlines.Add(run);
richTextBlock.Blocks.Add(paragraph);

// Add the RichTextBlock to the visual tree (assumes stackPanel is decalred in XAML).
//childStackPanel.Children.Add(richTextBlock);
//rootStackPanel.Children.Add(richTextBlock);

但是,它不影响字体大小.

But, it is not affecting the font size.

请帮帮我.谢谢.

推荐答案

您可以在 ViewModel 或代码隐藏文件中创建一个属性,并使用数据绑定将其连接到 FontSize 属性.

You can create a Property in your ViewModel or code behind file and use data binding to connect it to the FontSize property.

private double _myFontSize;    
public double MyFontSize
{
   get{ return _myFontSize;  }
   set{ _myFontSize = value; }
}

现在在您的数据模板中设置富文本框的字体大小,

Now inside your data template for the font size of the rich text box,

FontSize="{Binding MyFontSize, ElementName=page/viewModel}"

并记住使用 'INotifyPropertyChanged' 在您的属性值更改时通知 UI.这个网站提供了实施指南'INotifyPropertyChanged'

And remember to use 'INotifyPropertyChanged' to notify the UI when the value of your property changes. This web site provides a guide on implementing 'INotifyPropertyChanged'

这篇关于在uwp中动态设置字体大小为richtexblock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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