简体   繁体   English

在插入点将“ FontWeightProperty”设置为BOLD

[英]set “FontWeightProperty” to BOLD in the insertion point

I use to make decoration toggle button for my rich text box in wpf my XAML code for button is : 我用来在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>

and I wrote this code in VS for "ToggleButton_Click" event : 我在VS中为“ ToggleButton_Click”事件编写了此代码:

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

and this for when user click somewhere in the rich text box the toggle button change according to what is the clicked text font weight is : 当用户单击富文本框中的某个位置时,切换按钮会根据单击的文本字体粗细为多少而发生变化:

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

all the thing work fine you can bold selection text and insertion point at the end of line BUT when you change the carter position to the middle of the text and change toggle button to make insertion bold all the text get bold and vice versa. 一切正常,当您将卡特位置更改为文本的中间并更改切换按钮以使插入的文本变为粗体时,可以在行的末尾加粗选择文本和插入点,反之亦然。 I search for long time but no answer can help me to make the insertion point font weight bold without changing other text.THX for your helps 我搜索了很长时间,但是没有答案可以帮助我在不更改其他文本的情况下使插入点字体的粗体变为粗体。

Its not changing the entire text just the work that has caret and I think that is how it is intended to work. 它不会改变整个文本,而只是改变有插入符号的工作,我想这就是它的工作方式。

if you check for if(richTxtBoxQ.Selection.IsEmpty) before applying the text decoration you can get rid of the issue. 如果在应用文本修饰之前检查if(richTxtBoxQ.Selection.IsEmpty) ,则可以摆脱此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM