如何通过模板在TextBox和RichTextBox之间切换

时间:2012-07-27 09:11:55

标签: c# .net wpf richtextbox controltemplate

我想编写一个TextBox控件,它在焦点失焦时是一个RichTextBox,当它聚焦时它变成一个TextBox。

我尝试了几种方法,但它们都没有工作。唯一可行的解​​决方案就是这样:

<RichTextBox x:Name="headText" Document="{Binding HeadDocument}" GotFocus="headText_GotFocus" LostFocus="headText_LostFocus">
        <Controls:HyperlinkRichTextBox.Resources>
            <ControlTemplate x:Key="headTemplate">
                <TextBox Text="{Binding Head}" />
            </ControlTemplate>
        </Controls:HyperlinkRichTextBox.Resources>
</RichTextBox>

并在后面的代码中:

private ControlTemplate oldTemplate;

private void headText_GotFocus(object sender, RoutedEventArgs e)
{
    var controlTemplate = headLineText.FindResource("headTemplate") as ControlTemplate;
    oldTemplate = headText.Template;
    headText.Template = controlTemplate;
    headText.Focus();
}

private void headText_LostFocus(object sender, RoutedEventArgs e)
{
    headText.Template = oldTemplate;
}

问题是虽然文本框在两种状态之间切换,但我无法在文本框中写入任何内容!

2 个答案:

答案 0 :(得分:1)

这篇文章可以帮助您入门:http://weblogs.asp.net/thomaslebrun/archive/2009/03/24/wpf-how-to-develop-and-editable-textblock.aspx

这是一个改编,你需要添加TextBox和RichTextBox的值绑定部分:

XAML

<Grid>
    <Grid.Resources>
        <Style TargetType="{x:Type local:EditableTextBlock}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:EditableTextBlock}">
                        <Grid x:Name="PART_GridContainer">
                            <RichTextBox x:Name="PART_TbDisplayText" Visibility="Visible" />
                            <TextBox x:Name="PART_TbEditText" Visibility="Hidden" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="BorderBrush" Value="Red" />
        </Style>
        <Style TargetType="RichTextBox">
            <Setter Property="BorderBrush" Value="Blue" />
        </Style>
    </Grid.Resources>
    <StackPanel>
        <local:EditableTextBlock />
        <Button>Lose focus</Button>
    </StackPanel>
</Grid>

代码:

[TemplatePart(Type = typeof(Grid), Name = EditableTextBlock.GRID_NAME)]
[TemplatePart(Type = typeof(RichTextBox), Name = EditableTextBlock.RICHTEXTBOX_DISPLAYTEXT_NAME)]
[TemplatePart(Type = typeof(TextBox), Name = EditableTextBlock.TEXTBOX_EDITTEXT_NAME)]
public class EditableTextBlock : Control
{
    private const string GRID_NAME = "PART_GridContainer";
    private const string RICHTEXTBOX_DISPLAYTEXT_NAME = "PART_TbDisplayText";
    private const string TEXTBOX_EDITTEXT_NAME = "PART_TbEditText";

    private Grid gridContainer;
    private RichTextBox richTextBox;
    private TextBox textBox;

    static EditableTextBlock()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(EditableTextBlock), new FrameworkPropertyMetadata(typeof(EditableTextBlock)));
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        this.gridContainer = this.Template.FindName(GRID_NAME, this) as Grid;

        if (this.gridContainer != null)
        {
            this.richTextBox = this.gridContainer.Children[0] as RichTextBox;
            this.richTextBox.GotFocus += new RoutedEventHandler(richTextBoxGotFocus);
            this.textBox = this.gridContainer.Children[1] as TextBox;
            this.textBox.LostFocus += this.textBoxLostFocus;
        }
    }

    private void richTextBoxGotFocus(object sender, RoutedEventArgs e)
    {
        this.richTextBox.Visibility = Visibility.Hidden;
        this.textBox.Visibility = Visibility.Visible;
        this.textBox.Focus();
    }

    private void textBoxLostFocus(object sender, RoutedEventArgs e)
    {
        this.richTextBox.Visibility = Visibility.Visible;
        this.textBox.Visibility = Visibility.Hidden;
    }
}

答案 1 :(得分:1)

这只是一个猜测,但我会说,因为你完全改变了富文本框的模板。请记住,ControlTemplate是您的控件组成方式。许多控件允许您完全修改它,但有时包含以“PART_XYZ”命名的元素,这些元素表示“如果您删除我,我的父模板化控件将无法正常运行”。我现在的猜测是,您似乎扩展的RichtTextBox将找不到任何必要的PART_控件并进入禁用模式,因此您的包含文本框也被禁用。

我的建议是,使用UserControl切换模板。提供2个模板,一个带有文本框,一个带有富文本框。