设置" FontWeightProperty"插入点

时间:2016-09-12 08:48:15

标签: c# wpf richtextbox

我用wpf为我的富文本框制作装饰切换按钮我的XAML代码为按钮是:

<ToggleButton x:Name="TbtnBold" HorizontalAlignment="Right" Height="24" Margin="0,84,318,0" VerticalAlignment="Top" Width="24" Click="ToggleButton_Click">
        <Image RenderOptions.BitmapScalingMode="Fant" Source="Pics/Icons/bold.png" Width="15" Height="15"/>
        </ToggleButton>

我在VS中编写了这段代码,用于&#34; ToggleButton_Click&#34;事件:

if (TbtnBold.IsChecked == true)
            {
                if (richTxtBoxQ.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && ((FontWeight)richTxtBoxQ.Selection.GetPropertyValue(Run.FontWeightProperty)) == FontWeights.Normal)
                    richTxtBoxQ.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
                else
                    richTxtBoxQ.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal); 
            if (TbtnBold.IsChecked == false)                                                      
            {
                if (richTxtBoxQ.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && ((FontWeight)richTxtBoxQ.Selection.GetPropertyValue(Run.FontWeightProperty)) == FontWeights.Bold)
                    richTxtBoxQ.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal);
                else
                    richTxtBoxQ.Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
            }

这是因为当用户点击富文本框中的某个位置时,切换按钮会根据所点击的文本字体重量而改变:

private void richTxtBoxQ_SelectionChanged(object sender, RoutedEventArgs e)
    {
        if (richTxtBoxQ.Selection.GetPropertyValue(Run.FontWeightProperty) is FontWeight && ((FontWeight)richTxtBoxQ.Selection.GetPropertyValue(Run.FontWeightProperty)) == FontWeights.Normal)
        {
            TbtnBold.IsChecked = false;
        }
        else
        {
            TbtnBold.IsChecked = true;
        }
    }

所有工作都很好你可以在行尾添加粗体选择文本和插入点但是当你将卡特位置改为文本的中间并改变切换按钮以使插入变粗时所有文本都变为粗体,反之亦然。 我搜索很长时间,但没有答案可以帮助我使插入点字体粗体粗体而不更改其他text.THX为您的帮助

0 个答案:

没有答案