Enable the controls in the Richtextbox

本文介绍了一个技巧,可以在WPF的RichTextBox中使用FlowDocument时使包含的控件如CheckBox和Button保持可用状态。通过继承FlowDocument并覆盖IsEnabledCore方法返回true来实现。文中提供了一个C#代码示例和XAML代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF FlowDocuments can contain controls like CheckBoxes and Buttons. When the document is displayed statically and is not editable the controls are enabled, however in WPF V1 when they are rendered as the content of a RichTextBox (and the FlowDocument can be edited) the controls are disabled.

This has not always been the case. During the WPF CTP releases around beta 1 it was possible to enable controls inside a FlowDocument contained within a RichTextBox by setting the IsEnabled property on the FlowDocument to true. As WPF V1 approached the WPF team decided to disable this functionality. Although I don’t believe I’ve ever seen a reason for this it would be a difficult feature to fully support.

Fortunately there is a fairly clean work-around, for those prepared to take the risks associated with using a feature that the product team has consciously disabled. By inheriting from FlowDocument and over-riding the protected IsEnabledCore method to return true, and then using this FlowDocument sub-type inside the RichTextBox you can add enabled controls.

C# Code (WPF V1)
class EnabledFlowDocument : FlowDocument
{
    protected override bool IsEnabledCore
    {
        get
        {
            return true;
        }
    }
}
Xaml Code (WPF V1)



 
  This is some text inside a sub-type of flowdocument
 
 
   
 
 
   
     NavigateUri="http://learnwpf.com"   Click="LinkClicked">
     visit LearnWPF.com
   
 


You can download the source code here

Note: I had to explicitly set the FontFamily and FontSize on the instance of the sub-type of FlowDocument that I created to match the “default” appearance of the regular FlowDocument, so a little more work is required to make this a drop-in replacement for the regular FlowDocument.

I first saw this work-around in the MSDN forums but have subsequently discovered it has been documented elsewhere.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值