我使用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吗?
答案 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;
}