访问windows phone 7中按钮控件模板内的richtextblock

时间:2013-09-30 04:38:34

标签: c# windows-phone-7 richtextbox

我使用C#从Windows Phone 7开发应用程序。 我想访问一个按钮模板中的richtextbox但由于某种原因VS2010无法识别richtextblock

<Button Name="btnFrom" Click="SetFromCurrency">
            <Button.Template>
                <ControlTemplate>
                    <RichTextBox x:Name="FromTxtBx" IsReadOnly="True" Height="55" Margin="15,219,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="185" Foreground="White" Background="White"   />
                </ControlTemplate>
            </Button.Template>
        </Button>

当我输入例如FromTxtBx.Text时,编译器会给我一个错误说(当前上下文中不存在名称'FromTxtBx')

有人能给我正确的方法来访问richtextbox吗?

1 个答案:

答案 0 :(得分:0)

您可能在ContentPresenter内有ControlTemplate个元素,然后将RichTextBox放入ContentPresenter

<ContentControl x:Name="ContentContainer">
       <ContentPresenter ></ContentPresenter>
</ContentControl>

之后,您可以使用Content属性访问按钮控件的内容。 例如:

if( (btnFrom.Content as RichTextBox) != null)
{
    (btnFrom.Content as RichTextBox).IsReadOnly = false;
}